microsoft / microsoft/TypeScript
Type variables in type abstractions are not properly concretized
@weswigham is already working on this.
Since Jan 24, 2025.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
### 🔎 Search Terms
type variables unknown
### 🕗 Version & Regression Information
- This changed between versions 4.9.5 and 5.0.4.
### ⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.7.3#code/MYewdgzgLgBAZjAvDAPADQDQD4AUBzALlQEFscAjItASiSxkxgitsXrxwmoG4AoX0JFgAnAKaxkcHCgBCZAB5EZregEYMMAETRhmnrwD0BmCZgA9APxA
### 💻 Code
```ts
const f = (g: (b: X) => X, s: X) => g(s);
const ret = f((x: B) => 1, "str");
// ^? const ret: unknown
```
### 🙁 Actual behavior
No type error is reported and `ret` is inferred as `unknown` type.
### 🙂 Expected behavior
I think a type error is reported because the call to `f` attempts to concretize the type variable X not only as a number but also as a string. In fact, TypeScript 4.9.5 reported a type error.
### Additional information about the issue
I am not sure that TypeScript 4.9.5 was perfect. The following code is inferred to be `unknown`, but I think `string` is correct. This behavior was back to TypeScript 3.5.1.
```ts
const f = (g: (b: X) => X, s: X) => g(s);
const ret = f((x: B) => x, "str");
// ^? const ret: unknown
// actual: ret is inferred as unknown
// expected: ret is inferred as string
```
https://www.typescriptlang.org/play/?ts=5.7.3#code/MYewdgzgLgBAZjAvDAPADQDQD4AUBzALlQEFscAjItASiSxkxgitsXrxwmoG4AoX0JFgAnAKaxkcHCgBCZAB5EZrevIwwARNGEaevAPT6YxmAD0A-P0MwAhsCgBXGwBsiY2AEsIMD2DijhMQATW28HMABrMBAAdzADI1F5AAdRe1Egt3Efb19-QIzQpihhXzwgA
I also found a strange behavior that a type variable that should have been concretized was returned without being concretized. I thought this is related, but I could be wrong. Sorry if so.
```ts
const f = (g: (x: X) => X) => g;
const h = f((x: number) => 1);
// ^? const h: (x: X) => X
h(1);
// actual: type error
// expected: no type error
```
https://www.typescriptlang.org/play/?ts=5.7.3#code/MYewdgzgLgBAZjAvDAPADQDQD4AUBzALlQEFscAPItASiSxhrpjxWgCcBLMPLAbgCh+oSLAAWSeCjABXALYAjAKZtcKAEJlKMGQuW1E9AIzUBAelMxLMAHoB+QaJzGzFgIbAo01wBsiUAJ4ADoowymwgbPzmoeTBHooAJkRgIDABwaFs4WxAA
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.