Skip to content

TypeScript class prototype includes instance member #30516

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

Closed
luokuning opened this issue Mar 21, 2019 · 4 comments
Closed

TypeScript class prototype includes instance member #30516

luokuning opened this issue Mar 21, 2019 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@luokuning
Copy link

TypeScript Version: TypeScript official playground

Search Terms:

Code

class Hello {
    abc = {
        a: 'l',
        b: 1,
    }
    k = (prop: keyof typeof Hello.prototype.abc) => {
        console.log(prop)
    }
}

Expected behavior:

TypeScript compiler should complain about the Hello.prototype.abc, because abc is an instance property not in the Hello.prototype.

Actual behavior:

The code works fine in TypeScript playground, no compiling errors.

Playground Link: http://www.typescriptlang.org/play/index.html#src=class%20Hello%20%7B%0D%0A%20%20%20%20abc%20%3D%20%7B%0D%0A%20%20%20%20%20%20%20%20a%3A%20'l'%2C%0D%0A%20%20%20%20%20%20%20%20b%3A%201%2C%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20k%20%3D%20(prop%3A%20keyof%20typeof%20Hello.prototype.abc)%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20%20%20console.log(prop)%0D%0A%20%20%20%20%7D%0D%0A%7D

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 21, 2019
@RyanCavanaugh
Copy link
Member

Duplicate #11558

@RyanCavanaugh
Copy link
Member

Also #20922

@jcalz
Copy link
Contributor

jcalz commented Mar 21, 2019

(I'm commenting here because the linked issues are locked)

One could imagine that instance-only properties could be made optional in the prototype type. But I'm sure this would break all kinds of things (I'm pretty sure x instanceof C narrows x to C['prototype'], which would be a funny thing to do if the prototype were wider than C) and not add much value.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants