microsoft / microsoft/TypeScript
Typescript fail to unify type variables
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
name: Bug
about: Create a report to help us improve TypeScript
title: Typescript fail to unify type variables
labels: ''
assignees: ''
Bug Report
🔎 Search Terms
unify type variables, unification, type inference.
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about Generics.
⏯ Playground Link
Playground link with relevant code
💻 Code
class Var {
id: number
name: string
static counter = 0
constructor(name: string) {
this.id = Var.counter++
this.name = name
}
}
function v(strs: TemplateStringsArray): Var {
const [name] = strs
return new Var(name)
}
type Logical<T> = Var | { [P in keyof T]: Logical<T[P]> }
type List<T> = null | { head: T; tail: List<T> }
function cons<T>(head: Logical<T>, tail: Logical<List<T>>): Logical<List<T>> {
return { head, tail }
}
function test(element: Logical<string>, list: Logical<List<string>>): void {
}
// This is Ok.
test("a", cons<string>(v`element`, v`tail`))
// But this is not Ok.
test("a", cons(v`element`, v`tail`))
🙁 Actual behavior
When I do not write the annotation for cons, an error occured:
Argument of type 'Logical<List<{ id: { toString: ...; toFixed: ...; toExponential: ...; toPrecision: ...; valueOf: ...; toLocaleString: ...; }; name: { toString: ...; charAt: ...; charCodeAt: ...; concat: ...; indexOf: ...; lastIndexOf: ...; ... 43 more ...; at: ...; }; }>>' is not assignable to parameter of type 'Logical<List<string>>'.
Type '{ head: Logical<{ id: { toString: ...; toFixed: ...; toExponential: ...; toPrecision: ...; valueOf: ...; toLocaleString: ...; }; name: { toString: ...; charAt: ...; charCodeAt: ...; concat: ...; indexOf: ...; lastIndexOf: ...; ... 43 more ...; at: ...; }; }>; tail: Logical<...>; }' is not assignable to type 'Logical<List<string>>'.
Type '{ head: Logical<{ id: { toString: ...; toFixed: ...; toExponential: ...; toPrecision: ...; valueOf: ...; toLocaleString: ...; }; name: { toString: ...; charAt: ...; charCodeAt: ...; concat: ...; indexOf: ...; lastIndexOf: ...; ... 43 more ...; at: ...; }; }>; tail: Logical<...>; }' is not assignable to type '{ head: Logical<string>; tail: Logical<List<string>>; }'.
Types of property 'head' are incompatible.
Type 'Logical<{ id: { toString: ...; toFixed: ...; toExponential: ...; toPrecision: ...; valueOf: ...; toLocaleString: ...; }; name: { toString: ...; charAt: ...; charCodeAt: ...; concat: ...; indexOf: ...; lastIndexOf: ...; ... 43 more ...; at: ...; }; }>' is not assignable to type 'Logical<string>'.
Type '{ id: Logical<{ toString: unknown; toFixed: unknown; toExponential: unknown; toPrecision: unknown; valueOf: unknown; toLocaleString: unknown; }>; name: Logical<{ toString: unknown; charAt: unknown; charCodeAt: unknown; concat: unknown; indexOf: unknown; lastIndexOf: unknown; localeCompare: unknown; ... 42 more ...; ...' is not assignable to type 'Logical<string>'.
Type '{ id: Logical<{ toString: unknown; toFixed: unknown; toExponential: unknown; toPrecision: unknown; valueOf: unknown; toLocaleString: unknown; }>; name: Logical<{ toString: unknown; charAt: unknown; charCodeAt: unknown; concat: unknown; indexOf: unknown; lastIndexOf: unknown; localeCompare: unknown; ... 42 more ...; ...' is not assignable to type 'Var'.
Types of property 'id' are incompatible.
Type 'Logical<{ toString: unknown; toFixed: unknown; toExponential: unknown; toPrecision: unknown; valueOf: unknown; toLocaleString: unknown; }>' is not assignable to type 'number'.
Type 'Var' is not assignable to type 'number'.
37 test("a", cons(v`element`, v`tail`))
~~~~~~~~~~~~~~~~~~~~~~~~~
🙂 Expected behavior
I expect type checker inference the type annotation for me, so I can write:
test("a", cons(v`element`, v`tail`))
Instead of:
test("a", cons<string>(v`element`, v`tail`))
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.
Research direction
Reproduce the behavior in the linked TypeScript Playground and compare the explicit type argument on cons with the inferred call in test. Start by examining how the generic cons, Logical, and List types interact; done means the unannotated call is accepted with the expected string-based type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100