-
-
Notifications
You must be signed in to change notification settings - Fork 677
Expand file tree
/
Copy pathis-writable-key-of.d.ts
More file actions
51 lines (38 loc) · 1.03 KB
/
is-writable-key-of.d.ts
File metadata and controls
51 lines (38 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import type {IsReadonlyKeyOf} from './is-readonly-key-of.d.ts';
import type {Not} from './internal/type.d.ts';
import type {IsAny} from './is-any.d.ts';
/**
Returns a boolean for whether the given key is a writable key of type.
This is useful when writing utility types or schema validators that need to differentiate `writable` keys.
@example
```
import type {IsWritableKeyOf} from 'type-fest';
type User = {
name: string;
surname: string;
readonly id: number;
};
type Admin = {
name: string;
id: string;
};
type T1 = IsWritableKeyOf<User, 'name'>;
//=> true
type T2 = IsWritableKeyOf<User, 'id'>;
//=> false
type T3 = IsWritableKeyOf<User, 'name' | 'id'>;
//=> boolean
type T4 = IsWritableKeyOf<User | Admin, 'name'>;
//=> true
type T5 = IsWritableKeyOf<User | Admin, 'id'>;
//=> boolean
```
@category Type Guard
@category Utilities
*/
export type IsWritableKeyOf<Type extends object, Key extends keyof Type> =
IsAny<Type | Key> extends true ? never
: Key extends keyof Type
? Not<IsReadonlyKeyOf<Type, Key>>
: false;
export {};