-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathset-readonly.d.ts
More file actions
40 lines (32 loc) · 1.08 KB
/
set-readonly.d.ts
File metadata and controls
40 lines (32 loc) · 1.08 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
import type {Except} from './except.d.ts';
import type {HomomorphicPick} from './internal/index.d.ts';
import type {Simplify} from './simplify.d.ts';
/**
Create a type that makes the given keys readonly. The remaining keys are kept as is.
Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are readonly.
@example
```
import type {SetReadonly} from 'type-fest';
type Foo = {
a: number;
readonly b: string;
c: boolean;
};
type SomeReadonly = SetReadonly<Foo, 'b' | 'c'>;
//=> {a: number; readonly b: string; readonly c: boolean}
```
@category Object
*/
export type SetReadonly<BaseType, Keys extends keyof BaseType> =
(BaseType extends (...arguments_: never) => any
? (...arguments_: Parameters<BaseType>) => ReturnType<BaseType>
: unknown)
& _SetReadonly<BaseType, Keys>;
export type _SetReadonly<BaseType, Keys extends keyof BaseType> =
BaseType extends unknown // To distribute `BaseType` when it's a union type.
? Simplify<
Except<BaseType, Keys>
& Readonly<HomomorphicPick<BaseType, Keys>>
>
: never;
export {};