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

Support computed class properties #2286

Open
jamiebuilds opened this issue Aug 18, 2016 · 9 comments
Open

Support computed class properties #2286

jamiebuilds opened this issue Aug 18, 2016 · 9 comments

Comments

@jamiebuilds
Copy link
Contributor

Computed properties in objects were added (#252), but computed properties in classes are still unsupported.

class Component {
  ["foo"]() {} // known
}
declare function foo(): string;
class Component {
  [foo()]() {} // unknown
}
@git-jiby-me
Copy link

@thejameskyle anything on this yet ?

@jamiebuilds
Copy link
Contributor Author

@git-jiby-me Not yet, we'll update the issue when progress has been made

@callumlocke
Copy link
Contributor

As this still isn't fixed, does anyone have a workaround?

I want to make an iterable class like this:

class Foo {
  [Symbol.iterator]() {
    // ...
  }
}

Is there any way to do it?

@vkurchatkin
Copy link
Contributor

vkurchatkin commented Apr 3, 2017

@callumlocke something like this:

class Foo {
  
  constructor() {
    (this: any)[Symbol.iterator] = this._iterator;
  }
  
  _iterator(): Iterator<number> {
    return (function *() {
      yield 1;
      yield 2;
      yield 3;
    }())
  }
  
  @@iterator(): Iterator<number> {
    throw new Error();
  }
}

@madbence
Copy link

madbence commented Apr 3, 2017

it's a bit annoying, but if you use babel for transpiling, you cannot use the @@iterator() syntax 😿

@vkurchatkin
Copy link
Contributor

In that case you can escape it using comments:

class Foo {
  
  constructor() {
    (this: any)[Symbol.iterator] = this._iterator;
  }
  
  _iterator(): Iterator<number> {
    return (function *() {
      yield 1;
      yield 2;
      yield 3;
    }())
  }
  /*::
  @@iterator(): Iterator<number> {
    throw new Error();
  }
  */
}

@dchambers
Copy link

On a related note, does anyone have a work-around that would allow me to create an isIterable function that Flow could used to disambiguates the type of the argument I pass to it, for example:

if (isIterable(val)) {
  // Flow should know that `val` has type `Iterable<T>` on this branch of the guard
}
else {
  // Vs type `T` on this branch of the guard
}

doesn't work when isIterable is defined in an obvious way like this:

const isIterable = <T> (obj: T | Iterable<T>): boolean =>
  typeof (obj: any)[Symbol.iterator] === 'function';

Any smart ideas?

@jedwards1211
Copy link
Contributor

computed properties would be needed for symbol property keys.
@thejameskyle babel currently parses type properties in brackets as ObjectTypeIndexers, which is kind of awkward because that's not really what [someSymbol]: someType is doing...

// for sequelize...

declare opaque type And = symbol;
declare opaque type Or = symbol;

declare interface WhereLogic {
  [And]?: WhereLogic,
  [Or]?: Array<WhereLogic>,
  [field: string]: any,
}

@somewhatabstract
Copy link

Would be useful for static computed properties too.

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

8 participants