镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

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:

Activity

  1. jcalz commented on Dec 1, 2020

    @jcalz
    Contributor

    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.

  2. a1245582339 commented on Dec 3, 2020

    @a1245582339

    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.

  3. RyanCavanaugh commented on Dec 3, 2020

    @RyanCavanaugh
    Member

    Anders Hejlsberg (@ahejlsberg) Seems like we should be providing a literal context here?

  4. ahejlsberg commented on Dec 7, 2020

    @ahejlsberg
    Member

    This is a duplicate of #41631.

  5. added
    DuplicateAn existing issue was already created
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Dec 7, 2020
  6. ahejlsberg commented on Dec 9, 2020

    @ahejlsberg
    Member

    Now implemented in #41891.

  7. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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