-
-
Notifications
You must be signed in to change notification settings - Fork 567
/
set-field-type.d.ts
57 lines (46 loc) · 1.57 KB
/
set-field-type.d.ts
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
52
53
54
55
56
57
import type {Simplify} from './simplify';
type SetFieldTypeOptions = {
/**
Preserve optional and readonly modifiers for properties being updated.
NOTE: Property modifiers will always be preserved for properties that are not being updated.
@default true
*/
preservePropertyModifiers?: boolean;
};
/**
Create a type that changes the type of the given keys.
Use-cases:
- Creating variations of a base model.
- Fixing incorrect external types.
@see `Merge` if you need to change multiple properties to different types.
@example
```
import type {SetFieldType} from 'type-fest';
type MyModel = {
readonly id: number;
readonly createdAt: Date;
updatedAt?: Date;
};
type MyModelApi = SetFieldType<MyModel, 'createdAt' | 'updatedAt', string>;
// {
// readonly id: number;
// readonly createdAt: string;
// updatedAt?: string;
// }
// `preservePropertyModifiers` option can be set to `false` if you want to remove property modifiers for properties being updated
type MyModelApi = SetFieldType<MyModel, 'createdAt' | 'updatedAt', string, {preservePropertyModifiers: false}>;
// {
// readonly id: number;
// createdAt: string; // no longer readonly
// updatedAt: string; // no longer optional
// }
```
@category Object
*/
export type SetFieldType<BaseType, Keys extends keyof BaseType, NewType, Options extends SetFieldTypeOptions = {preservePropertyModifiers: true}> =
Simplify<{
[P in keyof BaseType]: P extends Keys ? NewType : BaseType[P];
} & (
// `Record` is used to remove property modifiers
Options['preservePropertyModifiers'] extends false ? Record<Keys, NewType> : unknown
)>;