type-fest 3.11.1 → 3.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +1 -0
- package/package.json +1 -1
- package/readme.md +1 -0
- package/source/set-readonly.d.ts +38 -0
package/index.d.ts
CHANGED
|
@@ -33,6 +33,7 @@ export type {Promisable} from './source/promisable';
|
|
|
33
33
|
export type {Opaque, UnwrapOpaque} from './source/opaque';
|
|
34
34
|
export type {InvariantOf} from './source/invariant-of';
|
|
35
35
|
export type {SetOptional} from './source/set-optional';
|
|
36
|
+
export type {SetReadonly} from './source/set-readonly';
|
|
36
37
|
export type {SetRequired} from './source/set-required';
|
|
37
38
|
export type {SetNonNullable} from './source/set-non-nullable';
|
|
38
39
|
export type {ValueOf} from './source/value-of';
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -119,6 +119,7 @@ Click the type names for complete docs.
|
|
|
119
119
|
- [`UnwrapOpaque`](source/opaque.d.ts) - Revert an [opaque type](https://codemix.com/opaque-types-in-javascript/) back to its original type.
|
|
120
120
|
- [`InvariantOf`](source/invariant-of.d.ts) - Create an [invariant type](https://basarat.gitbook.io/typescript/type-system/type-compatibility#footnote-invariance), which is a type that does not accept supertypes and subtypes.
|
|
121
121
|
- [`SetOptional`](source/set-optional.d.ts) - Create a type that makes the given keys optional.
|
|
122
|
+
- [`SetReadonly`](source/set-readonly.d.ts) - Create a type that makes the given keys readonly.
|
|
122
123
|
- [`SetRequired`](source/set-required.d.ts) - Create a type that makes the given keys required.
|
|
123
124
|
- [`SetNonNullable`](source/set-non-nullable.d.ts) - Create a type that makes the given keys non-nullable.
|
|
124
125
|
- [`ValueOf`](source/value-of.d.ts) - Create a union of the given object's values, and optionally specify which keys to get the values from.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type {Except} from './except';
|
|
2
|
+
import type {Simplify} from './simplify';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Create a type that makes the given keys readonly. The remaining keys are kept as is.
|
|
6
|
+
|
|
7
|
+
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.
|
|
8
|
+
|
|
9
|
+
@example
|
|
10
|
+
```
|
|
11
|
+
import type {SetReadonly} from 'type-fest';
|
|
12
|
+
|
|
13
|
+
type Foo = {
|
|
14
|
+
a: number;
|
|
15
|
+
readonly b: string;
|
|
16
|
+
c: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type SomeReadonly = SetReadonly<Foo, 'b' | 'c'>;
|
|
20
|
+
// type SomeReadonly = {
|
|
21
|
+
// a: number;
|
|
22
|
+
// readonly b: string; // Was already readonly and still is.
|
|
23
|
+
// readonly c: boolean; // Is now readonly.
|
|
24
|
+
// }
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
@category Object
|
|
28
|
+
*/
|
|
29
|
+
export type SetReadonly<BaseType, Keys extends keyof BaseType> =
|
|
30
|
+
// `extends unknown` is always going to be the case and is used to convert any
|
|
31
|
+
// union into a [distributive conditional
|
|
32
|
+
// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
33
|
+
BaseType extends unknown
|
|
34
|
+
? Simplify<
|
|
35
|
+
Except<BaseType, Keys> &
|
|
36
|
+
Readonly<Pick<BaseType, Keys>>
|
|
37
|
+
>
|
|
38
|
+
: never;
|