microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.53k stars 12.43k forks source link

NoInfer doesn't work in string template types #59826

Open sparecycles opened 1 month ago

sparecycles commented 1 month ago

🔎 Search Terms

NoInfer string template

🕗 Version & Regression Information

This issue exists in every Playground-accessible version that has NoInfer.

⏯ Playground Link

here

💻 Code

function f<A>(a: NoInfer<A>, b: A extends string ? `rel:${A}` : never) {}
f("5", "rel:7"); // error - correct, type of A is "7"

but

function g<A>(a: A, b: A extends string ? `rel:${NoInfer<A>}` : never) {}
g("5", "rel:7"); // no error - surprising - type of A is inferred to be ("5" | "7")

🙁 Actual behavior

no type error on g() above, because the A type is being inferred via the string template in spite of the NoInfer marker.

🙂 Expected behavior

Both of the example calls should be recognized as wrong?

Additional information about the issue

Also tried

function g<A>(a: A, b: A extends string ? NoInfer<`rel:${A}`> : never) {}
function g<A>(a: A, b: NoInfer<A extends string ? `rel:${A}` : never>) {}

no luck.

Andarist commented 1 month ago

It's not about template literal types but rather about NoInfer used in a conditional type. This already doesn't work correctly:

function h<const A>(a: A, b: A extends string ? NoInfer<A> : never) {}
h("foo", "bar");
sparecycles commented 1 month ago

@Andarist you're absolutely right. Thanks for the fast fix!