Skip to content

Commit

Permalink
Jsonify: Convert undefined to null in union element of array (#901
Browse files Browse the repository at this point in the history
)
  • Loading branch information
m-shaka authored Jul 2, 2024
1 parent bdfc391 commit 60c1024
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion source/jsonify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {UnknownArray} from './unknown-array';
type NotJsonable = ((...arguments_: any[]) => any) | undefined | symbol;

type NeverToNull<T> = IsNever<T> extends true ? null : T;
type UndefinedToNull<T> = T extends undefined ? null : T;

// Handles tuples and arrays
type JsonifyList<T extends UnknownArray> = T extends readonly []
Expand All @@ -20,7 +21,7 @@ type JsonifyList<T extends UnknownArray> = T extends readonly []
? [NeverToNull<Jsonify<F>>, ...JsonifyList<R>]
: IsUnknown<T[number]> extends true
? []
: Array<T[number] extends NotJsonable ? null : Jsonify<T[number]>>;
: Array<T[number] extends NotJsonable ? null : Jsonify<UndefinedToNull<T[number]>>>;

type FilterJsonableKeys<T extends object> = {
[Key in keyof T]: T[Key] extends NotJsonable ? never : Key;
Expand Down
6 changes: 6 additions & 0 deletions test-d/jsonify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ expectType<never>(plainSymbol);
declare const arrayMemberUndefined: Jsonify<Array<typeof undefined>>;
expectType<null[]>(arrayMemberUndefined);

declare const arrayMemberUnionWithUndefined: Jsonify<Array<typeof undefined | typeof number>>;
expectType<Array<null | number>>(arrayMemberUnionWithUndefined);

declare const arrayMemberUnionWithUndefinedDeep: Jsonify<Array<Array<typeof undefined | typeof number>> | {foo: Array<typeof undefined | typeof number>}>;
expectType<Array<Array<null | number>> | {foo: Array<null | number>}>(arrayMemberUnionWithUndefinedDeep);

declare const arrayMemberFunction: Jsonify<Array<typeof function_>>;
expectType<null[]>(arrayMemberFunction);

Expand Down

0 comments on commit 60c1024

Please sign in to comment.