-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Template literal wrong inferred value when multiple placeholders #47048
Copy link
Copy link
Closed
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Description
Bug Report
🔎 Search Terms
template literal wrong infer
🕗 Version & Regression Information
Tested in version 4.1.5, 5.2.3, 4.3.5, 4.4.0, 4.5.2, 4.6.0 nightly, everywhere the same behaviour.
⏯ Playground Link
Playground link with relevant code
💻 Code
type d8 = `1234` extends `${number}${infer T1}${number}` ? T1 : never; //'2'
type d9 = `2234` extends `${number}${infer T1}${number}` ? T1 : never; //never🙁 Actual behavior
d9 is computed as never.
🙂 Expected behavior
d9 should be computed as '2'.
After debugging a bit inferFromLiteralPartsToTemplateLiteral in checker.ts, it seems p = getSourceText(s).indexOf(delim, p) searches for 2 in a targetTexts being ['', 2, ''] yielding a wrong result.
It's not clear to me what the actual algorithm should be here as I haven't found any spec or fundamental rules behind inferring template placeholders like above.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug