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

Add a constraint for exact types #6747

Closed
jcready opened this issue Aug 16, 2018 · 4 comments
Closed

Add a constraint for exact types #6747

jcready opened this issue Aug 16, 2018 · 4 comments

Comments

@jcready
Copy link
Contributor

jcready commented Aug 16, 2018

I have a generic function that by design requires that its parameters may only take exact arguments, i wish i could get such guarantee from flow by declaring my function as follows:

declare class Object {
  static entries<exact T>(object: T): Array<[$Keys<T>, $Values<T>]>;
  static entries(object: any): Array<[string, mixed]>;
  static keys<exact T>(o: T): Array<$Keys<T>>;
  static keys(o: any): Array<string>;
  static values<exact T>(object: T): Array<$Values<T>>;
  static values(object: any): Array<mixed>;
}
@goodmind
Copy link
Contributor

goodmind commented Jun 9, 2019

I think this is flawed idea, because exact object is more like labeled tuple, so you can't really have supertype of all exacts

Related #7773

@jcready
Copy link
Contributor Author

jcready commented Jun 11, 2019

What is the proposed alternative solution to properly typing Object.values() and Object.entries() so they don't result in mixed?

@goodmind
Copy link
Contributor

OCaml :\

@eilvelia
Copy link

Currently the constraint exists, you can use $Exact for this:

image

declare function keys<O: $Exact<any>>(o: O): Array<$Keys<O>>;
declare function keys(o: mixed): Array<string>;

declare var o1: {a:1, ...}
declare var o2: {|a:1|}
const ex1 = keys({a:1})
const ex2 = keys(o2)
const inex1 = keys(o1)

const t11: 'a' = ex1[0] // ok
const t12: 'a' = ex2[0] // ok
const t13: 'a' = inex1[0] // error

try flow

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

3 participants