Skip to content

Template Literal Type - Does not evaluate interpolated template strings against type definition #41732

Closed
@dan-kez

Description

@dan-kez

TypeScript Version: 4.1.2

Search Terms:
template literal type template string interpolation number pixel value

Code

const pixelValue: number = 22;

type PixelValueType = `${number}px`;

const pixelString: PixelValueType = `22px`;

// Error here
const pixelStringWithTemplate: PixelValueType = `${pixelValue}px`;

Expected behavior:
A template string should be evaluated to determine if it matches a template literal type.

Actual behavior:
The template string is evaluated to type string even though it can match the Template Literal Type

Type 'string' is not assignable to type '`${number}px`'.(2322)

Playground Link:
https://www.typescriptlang.org/play?#code/MYewdgzgLgBADgSwB4FMA2A1AhmgrigLhjFwFsAjFAJxgF4YAmBgbgChWoBPOFGABWTpseFABVuvegAMAJAG8SFagF84SKW1ahIsRKjQBlKFQRgA5kQH7h+cTzowpTNRq3ho8QYeOmzAdQQoAAtRFFI4NCwoQn4vGzEJB1k5PSEcfFV1NiA

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions