-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OmitStrict type for when you don't want to be permissive. (#31)
I preferred the old behavior of `Omit` before it was made permissive, but I understand the use-cases for it. This PR re-introduces the original behavior under a different name. The benefit that a stricter type has is primarily: - Preventing typos. - Allowing the compiler to pick up on rename refactors automatically. Currently, permissive `Omit` acts as a "barrier" that prevents rename refactors from passing through, which means that any such refactor generates whole bunches of errors that have to be manually fixed. If the field in question is optional, this can actually introduce bugs. I'm open to bike-shedding the name, but I'd like to have both types available so I don't have to pin to an old version of the library or re-declare my own strict variants!
- Loading branch information
1 parent
f433ed5
commit 479541c
Showing
3 changed files
with
39 additions
and
5 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
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 |
---|---|---|
@@ -1,9 +1,28 @@ | ||
import { Omit } from 'type-zoo'; | ||
import { Omit, OmitStrict } from 'type-zoo'; | ||
|
||
declare const foo: Omit<{ x: boolean; y: string }, 'x'>; | ||
declare const omitExistingField: Omit<{ x: boolean; y: string }, 'x'>; | ||
|
||
// $ExpectType string | ||
foo.y; | ||
omitExistingField.y; | ||
|
||
// $ExpectError | ||
foo.x; | ||
omitExistingField.x; | ||
|
||
declare const omitNonexistentField: Omit<{ x: boolean }, 'y'>; | ||
|
||
// $ExpectType boolean | ||
omitNonexistentField.x; | ||
|
||
// $ExpectError | ||
omitNonexistentField.y; | ||
|
||
// $ExpectError | ||
declare const omitNonexistentFieldStrict: OmitStrict<{ x: boolean }, 'y'>; | ||
|
||
declare const omitExistingFieldStrict: OmitStrict<{ x: boolean; y: string }, 'x'>; | ||
|
||
// $ExpectType string | ||
omitExistingField.y; | ||
|
||
// $ExpectError | ||
omitExistingField.x; |