fix(types): `JSONParsed` supports interface and `Date` etc. (#1853)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(types): `JSONParsed` supports interface and `Date` etc. * denoify * supports bigint as never * denoify
authored by Yusuke Wada · committed by GitHub · parent 813484d · Browse files · 3 files·+95 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+95 added−6 removed
- tests:+71 added−0 removed
- other:+24 added−6 removed
Provenance
- Commit
bfd7583- Tree
ef441ab- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
813484d
3 files changed+95 added−6 removed
modified:deno_dist/utils/types.ts+12 added-3 removed
25 unmodified lines26272829303132303132333435363738394041424344459 unmodified lines25 unmodified linesexport type JSONPrimitive = string | boolean | number | null | undefinedexport type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[]export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject | object }export type JSONValue = JSONObject | JSONArray | JSONPrimitiveexport type JSONParsed<T> = { [k in keyof T]: T[k] extends JSONValue ? T[k] : string}// Non-JSON values such as `Date` implement `.toJSON()`, so they can be transformed to a value assignable to `JSONObject`:export type JSONParsed<T> = T extends { toJSON(): infer J } ? (() => J) extends () => JSONObject ? J : JSONParsed<J> : T extends JSONPrimitive ? T : T extends Array<infer U> ? Array<JSONParsed<U>> : T extends object ? { [K in keyof T]: JSONParsed<T[K]> } : never
export type InterfaceToType<T> = T extends Function ? T : { [K in keyof T]: InterfaceToType<T[K]> }
export type RequiredKeysOf<BaseType extends object> = Exclude<9 unmodified linesadded:src/utils/types.test.ts+71 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import type { Equal, Expect, JSONParsed } from './types'
describe('JSONParsed', () => { enum SampleEnum { Value1 = 'value1', Value2 = 'value2', }
interface Meta { metadata: { href: string sampleEnum: SampleEnum } }
interface SampleInterface { someMeta: Meta }
type SampleType = { someMeta: Meta }
it('Should parse a complex type', () => { const sample: JSONParsed<SampleType> = { someMeta: { metadata: { href: '', sampleEnum: SampleEnum.Value1, }, }, } expectTypeOf(sample).toEqualTypeOf<SampleType>() })
it('Should parse a complex interface', () => { const sample: JSONParsed<SampleInterface> = { someMeta: { metadata: { href: '', sampleEnum: SampleEnum.Value1, }, }, } expectTypeOf(sample).toEqualTypeOf<SampleInterface>() })
it('Should convert Date to string', () => { type Post = { datetime: Date } type Expected = { datetime: string } type Actual = JSONParsed<Post> // eslint-disable-next-line @typescript-eslint/no-unused-vars type verify = Expect<Equal<Expected, Actual>> })
it('Should convert bigint to never', () => { type Post = { num: bigint } type Expected = { num: never } type Actual = JSONParsed<Post> // eslint-disable-next-line @typescript-eslint/no-unused-vars type verify = Expect<Equal<Expected, Actual>> })})modified:src/utils/types.ts+12 added-3 removed
25 unmodified lines26272829303132303132333435363738394041424344459 unmodified lines25 unmodified linesexport type JSONPrimitive = string | boolean | number | null | undefinedexport type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[]export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject | object }export type JSONValue = JSONObject | JSONArray | JSONPrimitiveexport type JSONParsed<T> = { [k in keyof T]: T[k] extends JSONValue ? T[k] : string}// Non-JSON values such as `Date` implement `.toJSON()`, so they can be transformed to a value assignable to `JSONObject`:export type JSONParsed<T> = T extends { toJSON(): infer J } ? (() => J) extends () => JSONObject ? J : JSONParsed<J> : T extends JSONPrimitive ? T : T extends Array<infer U> ? Array<JSONParsed<U>> : T extends object ? { [K in keyof T]: JSONParsed<T[K]> } : never
export type InterfaceToType<T> = T extends Function ? T : { [K in keyof T]: InterfaceToType<T[K]> }
export type RequiredKeysOf<BaseType extends object> = Exclude<9 unmodified lines