microsoft / microsoft/TypeScript
Comparing constrained generic types/substitution types to conditional types
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
When comparing a generic type to a conditional type whose checkType is the same type, we have additional information that we are not utilizing.. for instance:
function f<T extends number>(x: T) {
var y: T extends number ? number : string;
// `T` is not assignable to `T extends number ? number : string`
y = x;
}
Ignoring intersections, we should be able to take the true branch all the time based on the constraint.
The intuition here is that T in the example above is really T extends number ? T : never which is assignable to T extends number ? number : string.
Similarly, with substitution types, we have additional information that we can leverage, e.g.:
declare function isNumber(a: any): a is number;
function map<T extends number | string>(
o: T,
map: (value: T extends number ? number : string) => any
): any {
if (isNumber(o)) {
// `T & number` is not assignable to `T extends number ? number : string`
return map(o);
}
}
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
The issue names no files or tests. Start by reproducing both TypeScript examples and trace the compiler's assignability handling for constrained generic and substitution types against conditional types; done means the shown assignments and call type-check as described.
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
- 35/100