-
-
Notifications
You must be signed in to change notification settings - Fork 568
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
LiteralToPrimitiveDeep
type (#584)
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
2e49866
commit 5374588
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type {LiteralToPrimitive} from './literal-to-primitive'; | ||
import type {OmitIndexSignature} from './omit-index-signature'; | ||
|
||
/** | ||
Like `LiteralToPrimitive` except it converts literal types inside an object or array deeply. | ||
For example, given a constant object, it returns a new object type with the same keys but with all the values converted to primitives. | ||
@see LiteralToPrimitive | ||
Use-case: Deal with data that is imported from a JSON file. | ||
@example | ||
``` | ||
import type {LiteralToPrimitiveDeep, TsConfigJson} from 'type-fest'; | ||
import tsconfig from 'path/to/tsconfig.json'; | ||
function doSomethingWithTSConfig(config: LiteralToPrimitiveDeep<TsConfigJson>) { ... } | ||
// No casting is needed to pass the type check | ||
doSomethingWithTSConfig(tsconfig); | ||
// If LiteralToPrimitiveDeep is not used, you need to cast the imported data like this: | ||
doSomethingWithTSConfig(tsconfig as TsConfigJson); | ||
``` | ||
@category Type | ||
@category Object | ||
*/ | ||
export type LiteralToPrimitiveDeep<T> = T extends object | ||
? T extends Array<infer U> | ||
? Array<LiteralToPrimitiveDeep<U>> | ||
: { | ||
[K in keyof OmitIndexSignature<T>]: LiteralToPrimitiveDeep<T[K]>; | ||
} | ||
: LiteralToPrimitive<T>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {expectType} from 'tsd'; | ||
import type {IsEqual, LiteralToPrimitiveDeep} from '../index'; | ||
|
||
type LiteralObject = { | ||
a: string; | ||
b: number; | ||
c: boolean; | ||
d: { | ||
e: bigint; | ||
f: symbol; | ||
g: { | ||
h: string[]; | ||
i: { | ||
j: boolean; | ||
k: { | ||
l: 1; | ||
m: 'hello'; | ||
o: [1, 2, 3]; | ||
p: ['a', 'b', 'c']; | ||
q: [1, 'a', true]; | ||
}; | ||
}; | ||
}; | ||
}; | ||
}; | ||
|
||
type PrimitiveObject = { | ||
a: string; | ||
b: number; | ||
c: boolean; | ||
d: { | ||
e: bigint; | ||
f: symbol; | ||
g: { | ||
h: string[]; | ||
i: { | ||
j: boolean; | ||
k: { | ||
l: number; | ||
m: string; | ||
o: number[]; | ||
p: string[]; | ||
q: Array<number | string | boolean>; | ||
}; | ||
}; | ||
}; | ||
}; | ||
}; | ||
|
||
const typeEqual: IsEqual< | ||
LiteralToPrimitiveDeep<LiteralObject>, | ||
PrimitiveObject | ||
> = true; | ||
expectType<true>(typeEqual); |