Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type Inference in for... in combined with typeof #12944

Open
aersam opened this issue Dec 15, 2016 · 1 comment
Open

Type Inference in for... in combined with typeof #12944

aersam opened this issue Dec 15, 2016 · 1 comment
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@aersam
Copy link

aersam commented Dec 15, 2016

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)

@ahejlsberg
Copy link
Member

I think the suggestion here is that a typeof x === "function" type guard should change the type of x to (typeof x) & Function under the guard. We currently do something similar for primitive types, but nothing specific for functions.

@ahejlsberg ahejlsberg added the Suggestion An idea for TypeScript label Dec 15, 2016
@RyanCavanaugh RyanCavanaugh added the In Discussion Not yet reached consensus label Dec 15, 2016
@mhegazy mhegazy added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Jan 23, 2017
@mhegazy mhegazy added this to the Community milestone Jan 23, 2017
@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants