microsoft / microsoft/TypeScript
Type Inference in for... in combined with typeof
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.1.4
The following code gives an error in TS 2.1:
It's not really nice code, but I think it should work never the less :)
for (var a in this) {
if (typeof this[a] === "function" && !ko.isObservable(this[a]))
this[a] = this[a].bind(this);
}
Expected behavior:
I would expect this[a] to be typed as this[keyof this] & Function:
for (var a in this) {
if (typeof this[a] === "function" && !ko.isObservable(this[a]))
this[a] =( <this[keyof this] & Function> this[a]).bind(this);
}
Actual behavior:
It gives an error, as of TS 2.1, it worked in TS 2.0 (because mapped types did not exist, this[a] was typed as any)
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 TypeScript 2.1 error with the provided for...in and typeof example, then compare its behavior with TypeScript 2.0. Trace type inference for this[a] through the for...in narrowing path; done means the example type-checks without the explicit cast while preserving the expected function narrowing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100