From 60c10242c5f74a012d7eebb89622552819f8f097 Mon Sep 17 00:00:00 2001 From: m-shaka Date: Tue, 2 Jul 2024 21:34:46 +0900 Subject: [PATCH] `Jsonify`: Convert `undefined` to `null` in union element of array (#901) --- source/jsonify.d.ts | 3 ++- test-d/jsonify.ts | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/source/jsonify.d.ts b/source/jsonify.d.ts index ef5d726dc..a1954cd20 100644 --- a/source/jsonify.d.ts +++ b/source/jsonify.d.ts @@ -12,6 +12,7 @@ import type {UnknownArray} from './unknown-array'; type NotJsonable = ((...arguments_: any[]) => any) | undefined | symbol; type NeverToNull = IsNever extends true ? null : T; +type UndefinedToNull = T extends undefined ? null : T; // Handles tuples and arrays type JsonifyList = T extends readonly [] @@ -20,7 +21,7 @@ type JsonifyList = T extends readonly [] ? [NeverToNull>, ...JsonifyList] : IsUnknown extends true ? [] - : Array>; + : Array>>; type FilterJsonableKeys = { [Key in keyof T]: T[Key] extends NotJsonable ? never : Key; diff --git a/test-d/jsonify.ts b/test-d/jsonify.ts index b8bc94d07..f505ca883 100644 --- a/test-d/jsonify.ts +++ b/test-d/jsonify.ts @@ -192,6 +192,12 @@ expectType(plainSymbol); declare const arrayMemberUndefined: Jsonify>; expectType(arrayMemberUndefined); +declare const arrayMemberUnionWithUndefined: Jsonify>; +expectType>(arrayMemberUnionWithUndefined); + +declare const arrayMemberUnionWithUndefinedDeep: Jsonify> | {foo: Array}>; +expectType> | {foo: Array}>(arrayMemberUnionWithUndefinedDeep); + declare const arrayMemberFunction: Jsonify>; expectType(arrayMemberFunction);