microsoft / microsoft/TypeScript
Assignability rule for conditional types needs to require check types and distributivity to be identical
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: master (e4718564e5b2c1f2e7bbef27fc480fd8172dfdc0)
Search Terms: assignable assignability conditional type check type checkType distributive distributivity identical unsound
Code
type MyElement<A> = [A] extends [[infer E]] ? E : never;
function oops<A, B extends A>(arg: MyElement<A>): MyElement<B> {
return arg; // compiles OK, expected compile error
}
oops<[number | string], [string]>(42).slice(); // runtime error
type MyAcceptor<A> = [A] extends [[infer E]] ? (arg: E) => void : never;
function oops2<A, B extends A>(arg: MyAcceptor<B>): MyAcceptor<A> {
return arg; // compiles OK, expected compile error
}
oops2<[number | string], [string]>((arg) => arg.slice())(42); // runtime error
type Dist<T> = T extends number ? number : string;
type Aux<A extends { a: unknown }> = A["a"] extends number ? number : string;
type Nondist<T> = Aux<{a: T}>;
function oops3<T>(arg: Dist<T>): Nondist<T> {
return arg; // compiles OK, expected compile error
}
oops3<number | string>(42).slice(); // runtime error
Expected behavior: Compile errors as marked.
Actual behavior: Compiles OK, runtime errors as marked.
Playground Link: link
Related Issues: None found
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
Start with the three TypeScript examples and the linked Playground reproduction, focusing on the marked assignments that compile without errors. Investigate how conditional types compare their check types and distributivity, then add compiler tests covering the expected errors and verify that the marked runtime failures are rejected.
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