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
{{ message }}
Repository navigation
Tag template of arrow function infers wrong type #36700
Search Terms: tag function, tagged template, template string
Code
{functiontag(strings: TemplateStringsArray, ...args: unknown[]): [TemplateStringsArray,unknown[]]{return[strings,args];}// ✅ inferred type: [TemplateStringsArray, unknown[]]constt=tag`123`;// console.log(t[2]); // it is correct that this would not compile}{consttag=(strings: TemplateStringsArray, ...args: unknown[])=>[strings,args];// 🛑 inferred type: (TemplateStringsArray | unknown[])[]constt=tag`123`;console.log(t[2]);// should not compile, but it does!}
Expected behavior:
The console.log(t[2]) statement should produce the following error:
Tuple type '[TemplateStringsArray, unknown[]]' of length '2' has no element at index '2'. ts(2493)
It looks like this type inferrence is at least consistent with calling the same function without using template syntax:
{functiontag(strings: TemplateStringsArray, ...args: unknown[]): [TemplateStringsArray,unknown[]]{return[strings,args];}// ✅ inferred type: [TemplateStringsArray, unknown[]]constt=tag`123`;// console.log(t[2]); // it is correct that this would not compile}{consttag=(strings: TemplateStringsArray, ...args: unknown[])=>[strings,args];// 🛑 inferred type: (TemplateStringsArray | unknown[])[]constt=tag`123`;console.log(t[2]);// should not compile, but it does!}{consttag=(strings: TemplateStringsArray, ...args: unknown[])=>[strings,args];// 🛑 inferred type: (TemplateStringsArray | unknown[])[]constt=tag(Object.assign(["123"],{raw: ["123"]}));console.log(t[2]);// should not compile, but it does!}
An additional type witness does solve the problem. But it should work without it.
{consttag=(strings: TemplateStringsArray, ...args: unknown[])=>[strings,args];// 🛑 inferred type: (TemplateStringsArray | unknown[])[]constt=tag`123`;console.log(t[2]);// should not compile, but it does!}{consttag=(strings: TemplateStringsArray, ...args: unknown[]): [TemplateStringsArray,unknown[]]=>[strings,args];// 🛑 inferred type: (TemplateStringsArray | unknown[])[]constt=tag`123`;console.log(t[2]);// it is correct that this does not compile}
I think this is down to typescript not inferring tuple return types at all, and therefore not being able to correctly intuit the response here. I think this is a feature request, rather than a bug then.
Consider this minimal example:
{// f: () => number[], rather than f: () => [1,2]constf=()=>{return[1,2]}}
You are absolutely right. Without an additional type hint, type inference for a plain function behaves exactly the same. I will close this issue (and I will not create a feature request).
Many thanks for your investigation!
{// f: () => number[], rather than f: () => [1,2]constf=()=>{return[1,2]}}{// f(): number[], perfectly consistentfunctionf(){return[1,2]}}
I think it would be risky, the change might not be source compatible regarding existing code bases. But currently I can't find a scenario where it would break existing code.
Activity
Zemnmez commentedon Feb 8, 2020
It looks like this type inferrence is at least consistent with calling the same function without using template syntax:
Playground Link
danieldietrich commentedon Feb 8, 2020
That's right.
An additional type witness does solve the problem. But it should work without it.
Playground Link
Zemnmez commentedon Feb 8, 2020
I think this is down to typescript not inferring tuple return types at all, and therefore not being able to correctly intuit the response here. I think this is a feature request, rather than a bug then.
Consider this minimal example:
Playground Link
danieldietrich commentedon Feb 8, 2020
You are absolutely right. Without an additional type hint, type inference for a plain function behaves exactly the same. I will close this issue (and I will not create a feature request).
Many thanks for your investigation!
Playground Link
Zemnmez commentedon Feb 8, 2020
np! I would love to see this inference too :)
danieldietrich commentedon Feb 8, 2020
I think it would be risky, the change might not be source compatible regarding existing code bases. But currently I can't find a scenario where it would break existing code.
Playground Link
Zemnmez commentedon Feb 8, 2020
looks like this is issue #25516
danieldietrich commentedon Feb 8, 2020
Yes, it is. I would love to see more precise type inference.