joarwilk / flowgen

Generate flowtype definition files from TypeScript
Other
658 stars 78 forks source link

Possible solution for Pick #97

Closed kevinbarabash closed 5 years ago

kevinbarabash commented 5 years ago

In https://www.typescriptlang.org/docs/handbook/advanced-types.html, Pick is defined as:

type Pick<T, K extends keyof T> = {
    [P in K]: T[P];
}

Translating this to flow yields:

type Pick<T, K: $Keys<T>> = {
    [K]: $ElementType<T, K>,
};

Example

goodmind commented 5 years ago

https://github.com/lttb/flown

kevinbarabash commented 5 years ago

I knew about https://github.com/piotrwitek/utility-types. It's great to see the same thing in the opposite direction. Is there any reason not to use flown to help flowgen add some more ✅the Supported column?

goodmind commented 5 years ago

Ah, this is flowgen repo, not flow 😬

I don't think I can add library to generated files, nor I think your Pick is correct, because it makes indexer object

Counterexample

kevinbarabash commented 5 years ago

That's too bad. It's because $ElementType<T, K> is doing $ElementType<T, "x" | "y">. That's unfortunate. TypeScript's combination of mapped types and conditional types is really powerful.

kevinbarabash commented 5 years ago

I don't think I can add library to generated files

Is it possible to insert an import (or require) at the top the generated file?

goodmind commented 5 years ago

It wouldn't work with flow-typed, because it doesn't support dependencies between packages.

Also flown Pick doesn't match with typescript one, which uses union, while flown uses tuple for keys, I can convert union to tuple, but then it wouldn't work with Pick<X, SomeTypeAlias>

goodmind commented 5 years ago

Flow utility types are pretty weak, they break too much, TypeScript's mapped + conditional types are more reliable really. This has to do with lazy evaluation vs eager evaluation, TypeScript uses latter, Flow can't because they have $Call which should handle polymorphism lazily

goodmind commented 5 years ago

If you're interested in more powerful things just check my PRs about utility types 😀

kevinbarabash commented 5 years ago

Wow! Tuple reduce and tuple spread seem pretty powerful in their own right, but should be even more powerful when used together.