Skip to content

Commit

Permalink
fix(remix-server-runtime): fix Jsonify for objects with toJSON fu…
Browse files Browse the repository at this point in the history
…nction
  • Loading branch information
MichaelDeBoey committed Oct 22, 2023
1 parent a24520c commit 7a448d1
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions packages/remix-server-runtime/jsonify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type Jsonify<T> =
IsAny<T> extends true ? any :

// toJSON
T extends { toJSON(): infer U } ? (U extends JsonValue ? U : unknown) :
T extends { toJSON(): infer U } ? (U extends JsonValue ? U : Jsonify<U>) :

// primitives
T extends JsonPrimitive ? T :
Expand Down Expand Up @@ -46,7 +46,7 @@ export type Jsonify<T> =

// value is always not JSON => true
// value is always JSON => false
// value is somtimes JSON, sometimes not JSON => boolean
// value is sometimes JSON, sometimes not JSON => boolean
// note: cannot be inlined as logic requires union distribution
type ValueIsNotJson<T> = T extends NotJson ? true : false;

Expand Down Expand Up @@ -146,8 +146,14 @@ type _tests = [
// toJson
Expect<Equal<Jsonify<{ toJSON(): "stuff" }>, "stuff">>,
Expect<Equal<Jsonify<Date>, string>>,
Expect<Equal<Jsonify<{ toJSON(): undefined }>, unknown>>,
Expect<Equal<Jsonify<{ toJSON(): Date }>, unknown>>,
Expect<Equal<Jsonify<{ toJSON(): undefined }>, never>>,
Expect<Equal<Jsonify<{ toJSON(): Date }>, string>>,
Expect<Equal<Jsonify<{ toJSON(): Map<unknown, unknown> }>, EmptyObject>>,
Expect<Equal<Jsonify<{ toJSON(): Int8Array}>, Record<string, number>>>,
Expect<Equal<Jsonify<{ toJSON(): [1, "two", Date, undefined, false]}>, [1, "two", string, null, false]>>,
Expect<Equal<Jsonify<{ toJSON(): [unknown, 1]}>, [unknown, 1]>>,
// @ts-expect-error
Expect<Equal<Jsonify<{ toJSON(): MyClass}>, {a: string}>>,
Expect<Equal<Jsonify<BooleanWithToJson>, string>>,


Expand All @@ -161,13 +167,27 @@ type _tests = [
// object
Expect<Equal<Pretty<Jsonify<{}>>, {}>>,
Expect<Equal<Pretty<Jsonify<{a: string}>>, {a: string}>>,
Expect<Equal<Pretty<Jsonify<{a: Date}>>, {a: string}>>,
Expect<Equal<Pretty<Jsonify<{a: Map<unknown, unknown>}>>, {a: EmptyObject}>>,
Expect<Equal<Pretty<Jsonify<{a: Int8Array}>>, {a: Record<string, number>}>>,
Expect<Equal<Pretty<Jsonify<{a: [1, "two", Date, undefined, false]}>>, {a: [1, "two", string, null, false]}>>,
Expect<Equal<Pretty<Jsonify<{a: [unknown, 1]}>>, {a: [unknown, 1]}>>,
// @ts-expect-error
Expect<Equal<Pretty<Jsonify<{a: MyClass}>>, {a: {a: string}}>>,
Expect<Equal<Pretty<Jsonify<{a: string | undefined}>>, {a?: string}>>,
Expect<Equal<Pretty<Jsonify<{a?: string | undefined}>>, {a?: string}>>,
Expect<Equal<Pretty<Jsonify<{a?: Date}>>, {a?: string}>>,
Expect<Equal<Pretty<Jsonify<{a?: Map<unknown, unknown>}>>, {a?: EmptyObject}>>,
Expect<Equal<Pretty<Jsonify<{a?: Int8Array}>>, {a?: Record<string, number>}>>,
Expect<Equal<Pretty<Jsonify<{a?: [1, "two", Date, undefined, false]}>>, {a?: [1, "two", string, null, false]}>>,
Expect<Equal<Pretty<Jsonify<{a?: [unknown, 1]}>>, {a?: [unknown, 1]}>>,
// @ts-expect-error
Expect<Equal<Pretty<Jsonify<{a?: MyClass}>>, {a?: {a: string}}>>,
Expect<Equal<Pretty<Jsonify<{a: string, b: string | undefined, c: undefined}>>, {a: string, b?: string}>>,
Expect<Equal<Pretty<Jsonify<{a: undefined}>>, {}>>,
Expect<Equal<Pretty<Jsonify<Record<string, any>>>, Record<string, any>>>,
Expect<Equal<Pretty<Jsonify<Record<string, number>>>, Record<string, number>>>,
Expect<MutualExtends<Jsonify<{payload: Record<string, any>}>, { payload: Record<string, any>}>>,
Expect<MutualExtends<Jsonify<{payload: Record<string, any>}>, {payload: Record<string, any>}>>,
Expect<Equal<Pretty<Jsonify<{
// Should be kept
requiredString: string;
Expand Down

0 comments on commit 7a448d1

Please sign in to comment.