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 21 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 |
---|---|---|
|
@@ -129,7 +129,8 @@ Click the type names for complete docs. | |
|
||
- [`EmptyObject`](source/empty-object.d.ts) - Represents a strictly empty plain object, the `{}` value. | ||
- [`IsEmptyObject`](source/empty-object.d.ts) - Returns a `boolean` for whether the type is strictly equal to an empty plain object, the `{}` value. | ||
- [`Except`](source/except.d.ts) - Create a type from an object type without certain keys. This is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys). | ||
- [`Except`](source/except.d.ts) - Create a type from an object type without certain keys. Use option `requireExactProps: true` to disallow assigning additional properties to this type. (Note that that any omitted properties in the resulting type will be present in suggestions as `undefined`.) | ||
This is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys). | ||
- [`Writable`](source/writable.d.ts) - Create a type that strips `readonly` from all or some of an object's keys. The inverse of `Readonly<T>`. | ||
- [`WritableDeep`](source/writable-deep.d.ts) - Create a deeply mutable version of an `object`/`ReadonlyMap`/`ReadonlySet`/`ReadonlyArray` type. The inverse of `ReadonlyDeep<T>`. Use `Writable<T>` if you only need one level deep. | ||
- [`Merge`](source/merge.d.ts) - Merge two types into a new type. Keys of the second type overrides keys of the first type. | ||
|
@@ -910,4 +911,4 @@ You can find some examples in the [TypeScript docs](https://www.typescriptlang.o | |
|
||
## License | ||
|
||
SPDX-License-Identifier: (MIT OR CC0-1.0) | ||
SPDX-License-Identifier: (MIT OR CC0-1.0) | ||
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. Don't make unrelated changes. 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. 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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. @sindresorhus Could you give me some advice on how to fix this? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,19 @@ 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. | ||
Create a type from an object type without certain keys. Use option `requireExactProps: true` to disallow assigning additional properties to this type. Note that any omitted properties in the resulting type will be present in suggestions 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. This text should be in the docs for
|
||
|
||
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. | ||
|
||
|
@@ -43,15 +54,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.
The description here should not be changed. The option is explained in the type docs. That's enough.