Flow: Dynamic $PropertyType<> key

Created on 8 Jun 2017  路  3Comments  路  Source: facebook/flow

Hi,

I have a User object:

type User = {
  name: string,
};

That has a get() function, that takes in parameter the key of an attribute on User and returns this attribute.
The function get is

User.prototype.get = (prop) => {
  return this[prop];
};

How can I write this fonction definition ?
Here's what I got so far:

type User = {
  name: string,
  get: (k: $Keys<User>) => any, // How can I change any to the correct property type ?
  // It would be great if I could do `get: (k: $Keys<User>) => $PropertyType<User, k>`
};

Thanks :)


Associated StackOverflow post.

Most helpful comment

$ElementType<T, K> landed, i guess this issue can be closed :tada:

All 3 comments

@agentcooper
Do you know why we still have to wait for this PR? :/

$ElementType<T, K> landed, i guess this issue can be closed :tada:

Was this page helpful?
0 / 5 - 0 ratings