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 expression is not assignable to matching template literal type #43243

Closed
earthlyreason opened this issue Mar 14, 2021 · 3 comments Β· Fixed by #43361
Closed

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

earthlyreason opened this issue Mar 14, 2021 · 3 comments Β· Fixed by #43361
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@earthlyreason
Copy link

earthlyreason commented Mar 14, 2021

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.

@earthlyreason earthlyreason changed the title Type '${string} abc' is not assignable to type '${string} ${string}'. Template literal expression is not assignable to matching template literal type Mar 14, 2021
@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Mar 15, 2021
@RyanCavanaugh
Copy link
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.

@earthlyreason
Copy link
Author

earthlyreason commented Mar 15, 2021

Ah, thanks @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.

@earthlyreason
Copy link
Author

My respect and thanks, @ahejlsberg. β˜€οΈ

@DanielRosenwasser DanielRosenwasser added Fixed A PR has been merged for this issue and removed In Discussion Not yet reached consensus labels May 1, 2021
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 4.3.0 milestone May 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants