ericelliott / rtype

Intuitive structural type notation for JavaScript.
MIT License
1.13k stars 38 forks source link

this keyword as a dynamic return type #147

Open Mouvedia opened 5 years ago

Mouvedia commented 5 years ago

related: #68

example

You have a fluent interface but one of the method X is slightly restrictive and doesn't return this but a subset of its this. In this case if the next method Y returns this it will share its constraints.

problem: if that method Y may also follow another method Z that returns this, its return type becomes dynamic. solution: introduce a this return type

NB

It would be impractical to require the user to provide all the possibilities as unions. It's the job of the static type checker; it should be able to infer the correct type from the current this.

proposal

example(name: Type) => this
ericelliott commented 5 years ago

Wasn't this resolved with the :: proposal?

ThisType::example(name: Type) => ThisType

And, if you want to document changes:

ThisType::example(name: Type) => ThisType & { foo: String }
Mouvedia commented 5 years ago

I assume that ThisType is akin to Foo and not your proposal for this

@ericelliott I am talking about a dynamic type. As explained here:

…granted that you have declared the newly bound interface.

As I said in the nota bene up there that would require to return unions. Also your example can't work precisely because the signature of the function that I am talking about is not bound (i.e it may have different call-sites).

What are our priorities? Are the use cases for this feature too rare?

PRO

CON

ericelliott commented 5 years ago

ThisType is a type variable. It does not need to be declared.

Essentially, it is the same thing as this, and even without explicitly adding a special this type, you could just use it, and it would be treated as a type variable:

this::method() => this

But you need the :: to use it. That's how we know you're working with this.

Mouvedia commented 5 years ago

But you need the :: to use it. That's how we know you're working with this.

I like this, it's explicit.

ThisType is a type variable.

I thought we encouraged type variables to be one char long:

By convention, type variables are single letters and lowercased…