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 aliases within classes #2642

Closed
dchambers opened this issue Oct 17, 2016 · 4 comments
Closed

Type aliases within classes #2642

dchambers opened this issue Oct 17, 2016 · 4 comments

Comments

@dchambers
Copy link

dchambers commented Oct 17, 2016

Flow could really do with a way to define type aliases within a class, for example;

class C<T> {
  type Ts = T[];
}

I've put a lot of effort into avoiding the need for this feature, but it's been really hard to avoid:

  1. I'd actually prefer to use functions, but functions don't support generics in the same way that classes and object types do, where you can pass in the types you want to be used.
  2. I tried constructing the class within a factory class that receives the generic arguments, and which has an anonymous function within the factory method that creates the class (genius I thought 😄) but this led to subtle errors that eventually caused me to give up on this approach.
  3. I tried living without type aliases, but this first led to me having almost unreadable type definitions, and finally led to me having to weaken some type definitions that had become hundreds of characters long, and were completely unreadable.

Nobody else has yet requested this feature yet, so maybe I'm just doing things wrong?

@sebnow
Copy link
Contributor

sebnow commented Jan 17, 2017

Quite useful, specifically for the use case of a type depending the type from polymorphic classes.

@akozhemiakin
Copy link

akozhemiakin commented Apr 25, 2017

It could also be helpfull in terms of this issue: #235. For example, now, as a workaround, it is suggested ( (in #307 by @jeffmo) to do something like that:

type funcArg = {a: string, b: string};
var myfunction = ({a, b}:funcArg) => { ... }

But currently it is not applicable to class methods because we can not define type aliases inside class definitions.

@xixixao
Copy link

xixixao commented May 25, 2017

Agreed with @sebnow, this would be helpful to simplify type annotations inside polymorphic classes.

@colin-han
Copy link

Agreed, this feature is very useful for generate type just like @dchambers mention.

Currently, I use a workaround like following. But it look is so ugly, and it may cause other compile error if there has more complex types.

class C<
    T,
    Ts: Array<T> = Array<T>
> { }

@SamChou19815 SamChou19815 closed this as not planned Won't fix, can't repro, duplicate, stale Mar 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants