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

Can't get InstanceType of own property within class method #34565

Closed
Alacrit opened this issue Oct 18, 2019 · 2 comments
Closed

Can't get InstanceType of own property within class method #34565

Alacrit opened this issue Oct 18, 2019 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@Alacrit
Copy link

Alacrit commented Oct 18, 2019

TypeScript Version: 3.6.4

Search Terms:
InstanceType of this

Code

class Test {

    public prop: StringConstructor = String;
    public test!: InstanceType<this['prop']>;

    public fn() {
        let v1: InstanceType<StringConstructor>,
            v2: InstanceType<this['prop']>,
            v3: this['prop'];

        v1 = 'str'; // OK
        v2 = 'str'; // Type '"str"' is not assignable to type 'InstanceType<this["prop"]>'.
        v3 = String; // OK, TS knows type of this['prop']
        this.test = 'str'; // Type '"str"' is not assignable to type 'InstanceType<this["prop"]>'.
    }

}

let obj = new Test();
obj.test = 'str'; // OK

Expected behavior:
InstanceType<this['prop']> as String

Actual behavior:
Error: Type '"str"' is not assignable to type 'InstanceType<this["prop"]>'.

@RyanCavanaugh
Copy link
Member

This is the intended behavioral difference between a this types and the alternative name Test['prop'] - a subtype of Test might have a more specific type of prop

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 30, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' 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
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants