You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/except.d.ts
+28-5
Original file line number
Diff line number
Diff line change
@@ -29,9 +29,22 @@ type Filtered = Filter<'bar', 'foo'>;
29
29
*/
30
30
typeFilter<KeyType,ExcludeType>=IsEqual<KeyType,ExcludeType>extendstrue ? never : (KeyTypeextendsExcludeType ? never : KeyType);
31
31
32
+
typeExceptOptions={
33
+
/**
34
+
Disallow assigning non-specified properties.
35
+
36
+
Note that any omitted properties in the resulting type will be present in autocomplete as `undefined`.
37
+
38
+
@default false
39
+
*/
40
+
requireExactProps?: boolean;
41
+
};
42
+
32
43
/**
33
44
Create a type from an object type without certain keys.
34
45
46
+
We recommend setting the `requireExactProps` option to `true`.
47
+
35
48
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.
36
49
37
50
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';
43
56
type Foo = {
44
57
a: number;
45
58
b: string;
46
-
c: boolean;
47
59
};
48
60
49
-
type FooWithoutA = Except<Foo, 'a' | 'c'>;
50
-
//=> {b: string};
61
+
type FooWithoutA = Except<Foo, 'a'>;
62
+
//=> {b: string}
63
+
64
+
const fooWithoutA: FooWithoutA = {a: 1, b: '2'};
65
+
//=> errors: 'a' does not exist in type '{ b: string; }'
66
+
67
+
type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>;
68
+
//=> {a: number} & Partial<Record<"b", never>>
69
+
70
+
const fooWithoutB: FooWithoutB = {a: 1, b: '2'};
71
+
//=> errors at 'b': Type 'string' is not assignable to type 'undefined'.
0 commit comments