Skip to content

Commit

Permalink
[lib] Rewrite Omit make it semi-homomorphic
Browse files Browse the repository at this point in the history
Summary:
We should rewrite it so that optionality and variance can be preserved.

Changelog: [libdef] We rewrite the `Omit` type implementation. Now it will preserve the optionality and the variance of the input object. [example](https://flow.org/try/#1N4Igxg9gdgZglgcxALlAJwKYEMwBcD6aArlLnALYYrgA2WAzvXGCADQgYAeOBARgJ74AJhhhYiNXClzEM7DFCLl602e0hQhcMtCw18ufgAcqyMTXpyQ5LEZNCDx0+cvzOJtBQW49AOkMm+AhEWGhCKmZ6lgC+6hAi1AEYAAQA6toAFgDyRjpQeskAvMnAADpQyckwEBAA-MjJ9DJwUAgA3OWVvKENiuS8GGgdUNHl5RpNyRANWeTaADzpuNm5cLo0rMmlIN1o2wB8RSXRbckA9GfJg2gQaMkD1ZibUBDJNNAIg1doN2j05WwQAA3QZMaDUGxNQYgaJAA)

Reviewed By: jbrown215

Differential Revision: D47165009

fbshipit-source-id: 55d5558bb8f6a25e83829bca74c3e7a256bd424c
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Jul 3, 2023
1 parent cd8bced commit 328d949
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/core.js
Expand Up @@ -2728,7 +2728,7 @@ type Pick<O: {...}, Keys: $Keys<O>> = {[key in Keys]: O[key]};
/**
* Omit specific fields from an object, e.g. Omit<O, 'foo' | 'bar'>
*/
type Omit<O: {...}, Keys: $Keys<O>> = {[key in Exclude<$Keys<O>, Keys>]: O[key]};
type Omit<O: {...}, Keys: $Keys<O>> = Pick<O, Exclude<$Keys<O>, Keys>>;

/**
* Construct an object type using string literals as keys with the given type,
Expand Down
6 changes: 3 additions & 3 deletions tests/mapped_type_utilities/mapped_type_utilities.exp
@@ -1,6 +1,6 @@
Error ------------------------------------------------------------------------------------------------------ omit.js:7:7

Cannot get `noFoo.foo` because property `foo` is missing in `Omit` [1]. [prop-missing]
Cannot get `noFoo.foo` because property `foo` is missing in `Pick` [1]. [prop-missing]

omit.js:7:7
7| noFoo.foo; // ERROR
Expand All @@ -14,7 +14,7 @@ References:

Error ---------------------------------------------------------------------------------------------------- omit.js:12:12

Cannot get `noFooOrBar.foo` because property `foo` is missing in `Omit` [1]. [prop-missing]
Cannot get `noFooOrBar.foo` because property `foo` is missing in `Pick` [1]. [prop-missing]

omit.js:12:12
12| noFooOrBar.foo; // ERROR
Expand All @@ -28,7 +28,7 @@ References:

Error ---------------------------------------------------------------------------------------------------- omit.js:13:12

Cannot get `noFooOrBar.bar` because property `bar` (did you mean `baz`?) is missing in `Omit` [1]. [prop-missing]
Cannot get `noFooOrBar.bar` because property `bar` (did you mean `baz`?) is missing in `Pick` [1]. [prop-missing]

omit.js:13:12
13| noFooOrBar.bar; // ERROR
Expand Down
2 changes: 2 additions & 0 deletions tests/mapped_type_utilities/omit.js
Expand Up @@ -13,3 +13,5 @@ noFooOrBar.foo; // ERROR
noFooOrBar.bar; // ERROR
noFooOrBar.baz; // OK
(noFooOrBar:{baz: number}); // OK

const o: Omit<{ foo?: string, bar: number; }, "bar"> = {}; // OK

0 comments on commit 328d949

Please sign in to comment.