Last active
March 9, 2022 10:44
-
-
Save ediblecode/467bc45460b1aa4bf73f45d8c80cbc96 to your computer and use it in GitHub Desktop.
Recursively add a property in TypeScript
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
import { type Primitive } from "type-fest"; | |
export type BuiltIns = Primitive | Date | RegExp; | |
/** | |
* Add's a numeric `order` property to all objects within arrays, recursively. | |
* Used for creating nodes from nested arrays when we want to keep the same order | |
* as the feed. | |
* | |
* Based on type-fest's `ReadonlyDeep` | |
* | |
* @see https://github.com/sindresorhus/type-fest/blob/main/source/readonly-deep.d.ts#L37 | |
*/ | |
export type OrderedDeep<O> = O extends BuiltIns | BuiltIns[] | |
? O | |
: O extends (infer U)[] | |
? (OrderedDeep<U> & { order: number })[] | |
: O extends object | |
? { [K in keyof O]: OrderedDeep<O[K]> } | |
: O; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment