Skip to content

Latest commit

History

History
30 lines (24 loc) · 787 Bytes

File metadata and controls

30 lines (24 loc) · 787 Bytes

JavaScript and TypeScript Notes

The following is a type definition for a conversion roundtrip from obj to json to obj.
@see {@link https://effectivetypescript.com/2020/04/09/jsonify/}

// export type JsonifySimple<T> = T extends { toJSON(): infer U }// ? U// : T extends object// ? { [k in keyof T]: Jsonify<T[k]> }// : T;exporttypeJsonify<T>=Textends{toJSON: (...args: any)=> infer R}
? Jsonify<R>
: TextendsArray<infer I>
? Array<Jsonify<I>>
: Textends(...args: any)=>any
? never
: Textendsobject
? {[KinkeyofT]: Kextendsstring|number ? Jsonify<T[K]> : never}
: T;

Get the single type of an array type e.g: User[] -> User

typeGetElementType<Textendsany[]>=Textends(infer U)[] ? U : never;