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

'this' type inference from assignments to prototype properties in TypeScript #13013

Closed
niokasgami opened this issue Dec 18, 2016 · 7 comments
Closed
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@niokasgami
Copy link

So I was working with a Library (Rpg Maker MV One's) who's really ES5.
I realized that you can still use Es5 code and this cool!
The problem is when declaring intellisenses on function who have "this" variable or instance variables.
It's will not throw errors but it's kinda hard to declare the type of the variables

TypeScript Version: 2.1.1

Code

 function Foo(){this.initialize.apply(this,arguments);}
 Foo.prototype.constructor = Foo
 Foo.prototype.initialize = function() {
  this._someVariable = 10; // <- Will work but it's kinda hard for tell the debugguer this variables have to be a number right? 
};

SO my suggestion was if this was possible to implement a option who permit to infering the "this" variable type by doing a code like this in function who use prototype like in ES5
this._aNumber: number = 10;

The Reason I'm doing Es5 is because it's common practice to not "wreck" out the Library that we works.
I could works with Pure Es5 but I love Typescript and I'm trying to find a good "inBetween" for it.

thanks for your further answer!

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Dec 18, 2016

We have the option of typing the value of this as of 2.0 - and you can ensure you don't forget to use it with the noImplicitThis flag. Just as a note, these parameters get stripped out and have no runtime impact.

I don't know if there's an easy way to ensure that all your functions have their this types inferred (@sandersn @mhegazy @billti @bowdenk7). We already have this in our support for Salsa, and I feel like this would greatly ease the migration in codebases.

@DanielRosenwasser DanielRosenwasser added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Dec 18, 2016
@DanielRosenwasser DanielRosenwasser changed the title This variables in function declarations 'this' type inference from assignments to prototype properties in TypeScript Dec 18, 2016
@niokasgami
Copy link
Author

@DanielRosenwasser thanks for your fast answer!
Although even if we use this...how do we actually use it? I'm unsure how to properly declare the this for have a type to it.
(I just don't know how to use it.)

can you show with this example?

function Example() { this.initialize.apply(this, arguments); }
Example.prototype.constructor = Example

Example.prototype.initialize = function () {
    this._variable = 10;
};

@ChiriVulpes
Copy link

interface IExample {
  _variable: number;
  initialize (): void;
}

function Example(this: IExample) { this.initialize.apply(this, arguments); }
Example.prototype.constructor = Example;

Example.prototype.initialize = function (this: IExample) {
    this._variable = 10;
};

This is how I would do it @niokasgami -- not sure if Daniel was inferring something beyond this.

@niokasgami
Copy link
Author

wow thanks @Aarilight !
it's seem a little overkill to have to do a method like this but hey this nice....I just hope they will have a nicer way to do that in the futur XD

@sandersn
Copy link
Member

If you use allowJs: true, then you can leave your old files with the .js extension. When checking JS files, Typescript understands that this inside a function like Foo.prototype.f = function () { ... is this: Foo.

However, it doesn't track assignments outside the constructor function, so it doesn't know about this._variable. That means that Typescript doesn't automatically understand a separate initialize function, unfortunately.

@RyanCavanaugh
Copy link
Member

This is reasonably-well supported in JS and I don't think we're going to make future improvements to prototype-assignment-based classes in TS

@sandersn
Copy link
Member

(In any case, this issue was fixed some time in 2017 or 2018.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants