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 like this not assignable to this type. #22426

Closed
Griffork opened this issue Mar 9, 2018 · 2 comments
Closed

Type like this not assignable to this type. #22426

Griffork opened this issue Mar 9, 2018 · 2 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@Griffork
Copy link

Griffork commented Mar 9, 2018

TypeScript Version:
Version 2.8.0-dev.20180302

Search Terms:
type, this

Simplest Code

class Thing<T> {
    doot: this;
    dothing(newthing: Thing<T>) {
        this.doot = newthing;
    }
}

Expected behavior:
No error
Actual behavior:
Error Type 'Thing' is not assignable to type 'this'.

Note
Since there is no 'typeof this' operator that I can use, I need to use this to represent types that have the same type as this.

Playground Link

Slimmed down real world example

Related Issues:
8164

@ghost
Copy link

ghost commented Mar 9, 2018

You just need to consistently use this in both places.

doot: this;
dothing(newthing: this) {
    this.doot = newthing;
}

The requirement is to prevent this.doot from referring to a Thing even when we're in a subclass and this should refer to the subclass.

class Thing {
    doot!: this;
    dothing(newthing: this) {
        this.doot = newthing as any; // There's a reason the cast is needed!
    }
}

class SubThing extends Thing {
    constructor(readonly foot: number) {};
}

const s = new SubThing();
s.dothing(new Thing());
console.log(typeof s.doot.foot === "number"); // Expected this to always be true!

@ghost ghost added the Question An issue which isn't directly actionable in code label Mar 9, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants