microsoft / microsoft/TypeScript

Type Inference in for... in combined with typeof

Open
#12,944 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Help Wanted Suggestion
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.