UNPKG

1.16 kBTypeScriptView Raw
1import type {LiteralToPrimitive} from './literal-to-primitive';
2import type {OmitIndexSignature} from './omit-index-signature';
3
4/**
5Like `LiteralToPrimitive` except it converts literal types inside an object or array deeply.
6
7For 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
11Use-case: Deal with data that is imported from a JSON file.
12
13@example
14```
15import type {LiteralToPrimitiveDeep, TsConfigJson} from 'type-fest';
16import tsconfig from 'path/to/tsconfig.json';
17
18function doSomethingWithTSConfig(config: LiteralToPrimitiveDeep<TsConfigJson>) { ... }
19
20// No casting is needed to pass the type check
21doSomethingWithTSConfig(tsconfig);
22
23// If LiteralToPrimitiveDeep is not used, you need to cast the imported data like this:
24doSomethingWithTSConfig(tsconfig as TsConfigJson);
25```
26
27@category Type
28@category Object
29*/
30export 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>;