microsoft / microsoft/TypeScript
Infer constrained generic parameters after instanceof check
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 2.4.0
When a variable passes an instanceof check generic type with constraints on the type parameter, apply to constraints to the variable. There is currently no clean way to handle the following without an explicit cast:
Code
interface Foo {
foo: string;
}
class Bar<T extends Foo> {
constructor(readonly bar: T) {}
}
let a: any;
if (a instanceof Bar) {
a.bar; // <-- a.bar should be 'Foo' instead of 'any'
}
Expected behavior:
a.bar is inferred as an instance of Foo.
Actual behavior:
a.bar is any
This change would only impact type checking, and will not affect compilation output.
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 TypeScript reproduction in the issue and inspect how the instanceof check narrows a variable whose generic type parameter has a constraint. Verify the inferred type of a.bar and identify the relevant type-checking test area. Done means a.bar is inferred as Foo without changing compilation output.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100