New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance Except
type
#560
Enhance Except
type
#560
Changes from 19 commits
5b3138b
fd7733a
29ce64d
03efc42
a0636bb
8bcc45c
3c5e005
0b5573b
3ef2af0
f14a038
d7a1d0f
dfb46ad
7aac550
3b30962
911352c
3432e46
5de63de
e2a2cc0
9d63b07
730d469
6ffbed6
880e7e4
87fcc50
af2e28b
f8e36b3
f5d2a10
0a15a94
fa7c464
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -29,9 +29,22 @@ type Filtered = Filter<'bar', 'foo'>; | |||||
*/ | ||||||
type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true ? never : (KeyType extends ExcludeType ? never : KeyType); | ||||||
|
||||||
type ExceptOptions = { | ||||||
/** | ||||||
Disallow assigning non-specified properties. | ||||||
|
||||||
Setting this to `false` is not recommended. Included for backwards-compatability. | ||||||
|
||||||
@default false | ||||||
*/ | ||||||
requireExactProps?: boolean; | ||||||
}; | ||||||
|
||||||
/** | ||||||
Create a type from an object type without certain keys. | ||||||
|
||||||
Use option `requireExactProps: true` to disallow assigning additional properties to this type. Notice that any omitted properties in the resulting type will be present as `undefined`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also add a note like this to the readme:
Which means line 46 could probably just be appended to line 44: /**
Create a type from an object type without certain keys. Use option `requireExactProps: true` to disallow assigning additional properties to this type. Notice that any omitted properties in the resulting type will be present as `undefined`.
This type is a stricter...
...
*/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||||||
|
||||||
This type is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type). The `Omit` type does not restrict the omitted keys to be keys present on the given type, while `Except` does. The benefits of a stricter type are avoiding typos and allowing the compiler to pick up on rename refactors automatically. | ||||||
|
||||||
This type was proposed to the TypeScript team, which declined it, saying they prefer that libraries implement stricter versions of the built-in types ([microsoft/TypeScript#30825](https://github.com/microsoft/TypeScript/issues/30825#issuecomment-523668235)). | ||||||
|
@@ -43,15 +56,25 @@ import type {Except} from 'type-fest'; | |||||
type Foo = { | ||||||
a: number; | ||||||
b: string; | ||||||
c: boolean; | ||||||
}; | ||||||
|
||||||
type FooWithoutA = Except<Foo, 'a' | 'c'>; | ||||||
//=> {b: string}; | ||||||
type FooWithoutA = Except<Foo, 'a'>; | ||||||
//=> {b: string} | ||||||
|
||||||
const fooWithoutA: FooWithoutA = {a: 1, b: '2'}; | ||||||
//=> errors: 'a' does not exist in type '{ b: string; }' | ||||||
|
||||||
type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>; | ||||||
//=> {a: number} & Partial<Record<"b", never>> | ||||||
|
||||||
const fooWithoutB: FooWithoutB = {a: 1, b: '2'}; | ||||||
//=> errors at 'b': Type 'string' is not assignable to type 'undefined'. | ||||||
``` | ||||||
|
||||||
@category Object | ||||||
*/ | ||||||
export type Except<ObjectType, KeysType extends keyof ObjectType> = { | ||||||
export type Except<ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = {requireExactProps: false}> = { | ||||||
[KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType]; | ||||||
}; | ||||||
} & (Options['requireExactProps'] extends true | ||||||
? Partial<Record<KeysType, never>> | ||||||
: {}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't make unrelated changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't figure out what was the change, maybe it's a white space?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the trailing new line was changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@orimiles5 This still needs to be fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sindresorhus Could you give me some advice on how to fix this?