1 | import type {LiteralToPrimitive} from './literal-to-primitive';
|
2 | import type {OmitIndexSignature} from './omit-index-signature';
|
3 |
|
4 | /**
|
5 | Like `LiteralToPrimitive` except it converts literal types inside an object or array deeply.
|
6 |
|
7 | For example, given a constant object, it returns a new object type with the same keys but with all the values converted to primitives.
|
8 |
|
9 | @see LiteralToPrimitive
|
10 |
|
11 | Use-case: Deal with data that is imported from a JSON file.
|
12 |
|
13 | @example
|
14 | ```
|
15 | import type {LiteralToPrimitiveDeep, TsConfigJson} from 'type-fest';
|
16 | import tsconfig from 'path/to/tsconfig.json';
|
17 |
|
18 | function doSomethingWithTSConfig(config: LiteralToPrimitiveDeep<TsConfigJson>) { ... }
|
19 |
|
20 | // No casting is needed to pass the type check
|
21 | doSomethingWithTSConfig(tsconfig);
|
22 |
|
23 | // If LiteralToPrimitiveDeep is not used, you need to cast the imported data like this:
|
24 | doSomethingWithTSConfig(tsconfig as TsConfigJson);
|
25 | ```
|
26 |
|
27 | @category Type
|
28 | @category Object
|
29 | */
|
30 | export type LiteralToPrimitiveDeep<T> = T extends object
|
31 | ? T extends Array<infer U>
|
32 | ? Array<LiteralToPrimitiveDeep<U>>
|
33 | : {
|
34 | [K in keyof OmitIndexSignature<T>]: LiteralToPrimitiveDeep<T[K]>;
|
35 | }
|
36 | : LiteralToPrimitive<T>;
|