-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[flow] Do not break up MaybeT, OptionalT on non-distributive conditio…
…nal type Summary: MaybeT and OptionalT should not be destructed unless we explicitly decide that it should have the distributive behavior: https://github.com/facebook/flow/blob/8e5ecac527cc76be12eae51a5219106dfbe8c69e/src/typing/flow_js.ml#L6554 https://github.com/facebook/flow/blob/8e5ecac527cc76be12eae51a5219106dfbe8c69e/src/typing/flow_js.ml#L6650-L6651 https://github.com/facebook/flow/blob/8e5ecac527cc76be12eae51a5219106dfbe8c69e/src/typing/flow_js.ml#L6659-L6660 In the main recursive flow_js, we did it correctly only for UnionT: https://github.com/facebook/flow/blob/8e5ecac527cc76be12eae51a5219106dfbe8c69e/src/typing/flow_js.ml#L1715-L1722 but missed the case for `MaybeT` and `OptionalT`. This diff fixes that. Changelog: [errors] Previously we incorrectly distribute-over-union for maybe or optional input types of conditional type. (e.g. `(?string) extends string ? true : false` is evaluated to `true | false` instead of `false`). This is now fixed, and code that depends on the bug might have new errors. Reviewed By: panagosg7 Differential Revision: D67145501 fbshipit-source-id: 573933e56fef794ee29709645d48d4754aa874c5
- Loading branch information
1 parent
1cd0a55
commit f543d19
Showing
3 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
21 changes: 21 additions & 0 deletions
21
tests/conditional_type/non_generic_maybe_nullable_tests.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
type MaybeStringExtendsString = (?string) extends string ? true : false; | ||
true as MaybeStringExtendsString; // error | ||
false as MaybeStringExtendsString; // ok | ||
|
||
function optionalTypeTest(x?: string) { | ||
type OptionalStringExtendsString = (typeof x) extends string ? true : false; | ||
true as OptionalStringExtendsString; // error | ||
false as OptionalStringExtendsString; // ok | ||
} | ||
|
||
type StringExtendsMaybeString = string extends (?string) ? true : false; | ||
true as StringExtendsMaybeString; // ok | ||
false as StringExtendsMaybeString; // error | ||
|
||
type StringOrNullOrVoidExtendsString = (string | null | void) extends string ? true : false; | ||
true as StringOrNullOrVoidExtendsString; // error | ||
false as StringOrNullOrVoidExtendsString; // ok | ||
|
||
type StringExtendsStringOrNullOrVoid = string extends (string | null | void) ? true : false; | ||
true as StringExtendsStringOrNullOrVoid; // ok | ||
false as StringExtendsStringOrNullOrVoid; // error |