You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeTemplated= `${string} ${string}`;constvalue1: string="abc";consttemplated1: Templated=`${value1} abc`asconst;// Type '`${string} abc`' is not assignable to type '`${string} ${string}`'.constvalue2="abc";consttemplated2: Templated=`${value2} abc`asconst;
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.
The text was updated successfully, but these errors were encountered:
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
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.
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}:
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.
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
π Search Terms
template literal types, template literal assignability
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
Note in the second case, where
value2
is a literal string, the assignment works okay.π Actual behavior
π Expected behavior
I would expect this value to be assignable, since the statically known
valuetype matches the template pattern.It is also odd that the two cases above behave differently.
The text was updated successfully, but these errors were encountered: