Skip to content

Template literal expression is not assignable to matching template literal typeΒ #43243

Description

Bug Report

πŸ”Ž Search Terms

template literal types, template literal assignability

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about template literal types.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Templated = `${string} ${string}`;

const value1: string = "abc";
const templated1: Templated = `${value1} abc` as const;
// Type '`${string} abc`' is not assignable to type '`${string} ${string}`'.

const value2 = "abc";
const templated2: Templated = `${value2} abc` as const;

Note in the second case, where value2 is a literal string, the assignment works okay.

πŸ™ Actual behavior

Type '`${string} abc`' is not assignable to type '`${string} ${string}`'.

πŸ™‚ Expected behavior

I would expect this value to be assignable, since the statically known value type matches the template pattern.

It is also odd that the two cases above behave differently.

Activity

  1. changed the title [-]Type '`${string} abc`' is not assignable to type '`${string} ${string}`'.[/-] [+]Template literal expression is not assignable to matching template literal type[/+] on Mar 14, 2021
  2. RyanCavanaugh commented on Mar 15, 2021

    @RyanCavanaugh
    Member

    We currently don't have any assignability rules to allow assigning one arbitrary template type to another (which is what's being suggested here). It's worth thinking about what rules, if any, would make sense to add.

  3. earthlyreason commented on Mar 15, 2021

    @earthlyreason
    Author

    Ah, thanks Ryan Cavanaugh (@RyanCavanaugh), that makes sense.

    This appears to be a generalization of the inferencing operation. Inferencing is able to do a kind of matching, but only when all parts are closed.

    If so, then this is related to #43060. If templates with open components could be inferred, then the same logic could be used to determine assignability.

    For example, this ad-hoc expression could check assignability to ${string} ${string}:

    type Assignable<T> = T extends `${infer A} ${infer B}`
      ? A extends string
        ? B extends string
          ? true
          : never
        : never
      : never;
    
    type V1 = Assignable<`${string} abc`>; // never
    type V2 = Assignable<`abc def`>; // true

    Intuitively, it seems that if a type can evaluate to true in an expression like the above (where the inner extends clauses represent the primitive or generic constraints in the template), then it is assignable to the template.

  4. earthlyreason commented on Mar 30, 2021

    @earthlyreason
    Author

    My respect and thanks, Anders Hejlsberg (@ahejlsberg). β˜€οΈ

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

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions