Flow: feature request: classes can reuse object type in definition

Created on 26 Oct 2016  路  3Comments  路  Source: facebook/flow

If I have

type o = {
 x: number
};

declare class ExampleClass {
 x: number;
};

it would be nice if I did not have to write this twice, and somehow reuse the object type def

object model feature request

Most helpful comment

any word on this feature request? This would save my life with immutable records

All 3 comments

Syntax suggestion:

type o = {
  x: number
};

declare class ExampleClass {
  ...o;
};

See: https://github.com/facebook/flow/issues/1326

any word on this feature request? This would save my life with immutable records

This would also make extending classes much easier, if you assume classes that have static or instance methods that rely on its own schema. In our particular use case, we autogenerate class definitions from the database, but occasionally need to override them for this and that:

declare class Model<Props> {
  static findOne(where: $Shape<Props>): Promise<?this>;
}
type UserProps = {|
  id: number,
  username: string,
  email: string,
|};

declare class User<Props = UserProps> extends Model<Props> {
  ...UserProps, // can't do this
}

type CustomerProps = {|
  ...$Exact<UserProps>,
  customerId: number,
|}
declare class Customer extends User<CustomerProps> {
  ...CustomerProps, // or this either
}
Was this page helpful?
0 / 5 - 0 ratings