Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dan-kez opened this issue Nov 30, 2020 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@dan-kez
Copy link

dan-kez commented Nov 30, 2020

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:

@jcalz
Copy link
Contributor

jcalz commented Dec 1, 2020

Related issues: #40707, #10676

From #40707, if you use a const assertion the compiler will give a template literal type to a template literal expression, resulting in your desired behavior:

const pixelValue: number = 22;
type PixelValueType = `${number}px`;
const pixelStringWithTemplate: PixelValueType = `${pixelValue}px` as const; // okay

Playground link

It looks like your desired behavior is for the compiler to always synthesize a template literal type for a template literal expression the way #10676 does for other literal types, so that as const is not necessary.

@a1245582339
Copy link

Related issues: #40707, #10676

From #40707, if you use a const assertion the compiler will give a template literal type to a template literal expression, resulting in your desired behavior:

const pixelValue: number = 22;
type PixelValueType = `${number}px`;
const pixelStringWithTemplate: PixelValueType = `${pixelValue}px` as const; // okay

Playground link

It looks like your desired behavior is for the compiler to always synthesize a template literal type for a template literal expression the way #10676 does for other literal types, so that as const is not necessary.

Why the ${pixelValue}px cannot be inferred to PixelValueType automatically? It seems nothing problem.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Dec 3, 2020
@RyanCavanaugh
Copy link
Member

@ahejlsberg Seems like we should be providing a literal context here?

@ahejlsberg
Copy link
Member

This is a duplicate of #41631.

@ahejlsberg ahejlsberg added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. labels Dec 7, 2020
@ahejlsberg ahejlsberg removed their assignment Dec 7, 2020
@ahejlsberg
Copy link
Member

Now implemented in #41891.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants