Fix: Ensure explicit undefined on optional members#2109
Open
huntie wants to merge 1 commit into
Open
Conversation
Summary: **Motivation** See react/react-native#57628. Flow optional object members permit `undefined` as a value, so the faithful TypeScript translation of `{foo?: T}` is `{foo?: T | undefined}`. The bare `{foo?: T}` form is stricter under TypeScript compared to Flow: ```js // Flow's semantics interface Props { onRefresh?: () => void; } const a: Props = { onRefresh: undefined }; // ✅ ok ``` ```ts // TypeScript with exactOptionalPropertyTypes: true (i.e. strict mode) interface Props { onRefresh?: () => void; } const a: Props = { onRefresh: undefined }; // ❌ error interface PropsFixed { onRefresh?: (() => void) | undefined; } const b: PropsFixed = { onRefresh: undefined }; // ✅ ok ``` **This diff** This makes the translator emit the explicit `| undefined` on optional members by default. Optional members (`?:`) now translate as wider resulting types: foo?: number => foo?: number | undefined foo?: () => void => foo?: (() => void) | undefined Members whose type already includes `undefined` (e.g. the maybe type `foo?: ?T` → `null | undefined | T`) are left unchanged (no duplicate `| undefined`). Required members are untouched. NOTE: **This is a behavior change**: 3 existing fixtures are updated. This is on-the-line for counting as semver breaking for this project — emitted types are now wider. Differential Revision: D113033807
|
@huntie has exported this pull request. If you are a Meta employee, you can view the originating Diff in D113033807. |
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:
Motivation
See react/react-native#57628.
Flow optional object members permit
undefinedas a value, so the faithful TypeScript translation of{foo?: T}is{foo?: T | undefined}.The bare
{foo?: T}form is stricter under TypeScript compared to Flow:This diff
This makes the translator emit the explicit
| undefinedon optional members by default. Optional members (?:) now translate as wider resulting types:foo?: number => foo?: number | undefined
foo?: () => void => foo?: (() => void) | undefined
Members whose type already includes
undefined(e.g. the maybe typefoo?: ?T→null | undefined | T) are left unchanged (no duplicate| undefined). Required members are untouched.NOTE: This is a behavior change: 3 existing fixtures are updated. This is borderline for counting as semver breaking for this project — emitted types are now wider.
Differential Revision: D113033807