UNPKG

744 BTypeScriptView Raw
1import type {Except} from './except';
2import type {Simplify} from './simplify';
3
4/**
5Create a type that changes the type of the given keys.
6
7Use-cases:
8- Creating variations of a base model.
9- Fixing incorrect external types.
10
11@see `Merge` if you need to change multiple properties to different types.
12
13@example
14```
15import type {SetFieldType} from 'type-fest';
16
17type MyModel = {
18 id: number;
19 createdAt: Date;
20 updatedAt: Date;
21};
22
23type MyModelApi = SetFieldType<MyModel, 'createdAt' | 'updatedAt', string>;
24// {
25// id: number;
26// createdAt: string;
27// updatedAt: string;
28// }
29```
30
31@category Object
32*/
33export type SetFieldType<BaseType, Keys extends keyof BaseType, NewType> =
34 Simplify<
35 Except<BaseType, Keys> &
36 Record<Keys, NewType>
37 >;