Prune no-op Omit keys from generated types#56824
Closed
huntie wants to merge 2 commits into
Closed
Conversation
Summary: Adds a `--watch` flag to `yarn build-types`. Helpful when debugging output TS types end-to-end. Changelog: [Internal] Reviewed By: robhogan Differential Revision: D104815967
Summary:
Update the `simplifyTypes` transform (Flow → TS generation) to prune/simplify `Omit<>` helpers in TypeScript `interface` unions.
This greatly reduces API snapshot churn in the next two diffs, which convert a number of `type` declarations (trivial unions) to `interface` (`Omit<>` required for correct union).
#### In detail
`flow-api-translator` emits `Omit<ParentType, keys>` to faithfully translate Flow's object spread override semantics to TypeScript.
```
// Flow
type Base = {x: string, y: number};
type Child = {...Base, x: boolean}; // Later properties in an object spread override earlier ones
// TypeScript
type Base = {x: string; y: number};
type Child = Omit<Base, "x"> & {x: boolean}; // Later properties in an object spread *merge*, rather than override. So we must use Omit<> for matched keys.
```
This adds a `collectTypeKeys` utility that recursively resolves all property keys reachable from a type, then uses it as a fallback in the existing `Omit` resolver to prune keys that don't exist on the parent type, or remove the `Omit` entirely when no keys overlap.
- See changes to `ReactNativeApi.d.ts` for examples.
- **Decision point**: This diff opts to preserve correctness in TS — even though the snapshot isn't/isn't intended to be read directly/programatically, vs the conciseness tradeoff if we dropped all `Omit<>`s (human readable but ambiguous to the typechecker).
Changelog: [Internal] - JS API snapshot changes are a simplification refactor only
Differential Revision: D105150070
|
@huntie has exported this pull request. If you are a Meta employee, you can view the originating Diff in D105150070. |
|
Warning JavaScript API change detected This PR commits an update to
This change was flagged as: |
|
This pull request has been merged in 0b9969f. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Update the
simplifyTypestransform (Flow → TS generation) to prune non-overlapping keys fromOmit<>helpers in TypeScriptinterfaceunions.This greatly reduces API snapshot churn/inflation in the next two diffs, which convert a number of
typedeclarations (trivial unions) tointerface(Omit<>required for correct union).In detail
flow-api-translatoremitsOmit<ParentType, keys>to faithfully translate Flow's object spread override semantics to TypeScript.For
interfacetype unions with overlapping keys, this can result in a number of unnecessary lines in the API snapshot, with little/no human readable value.This diff extends existing
Omit<>handling bysimplifyTypesto recursively resolve all property keys reachable from a type, and prune keys that don't exist on the parent type.ReactNativeApi.d.ts, P2325468482 for examples.Omit<>s (human readable but ambiguous to the typechecker).Changelog: [Internal] - JS API snapshot changes are a simplification refactor only
Differential Revision: D105150070