From 570c0b6d95f66239d7d004759fadfbf02610264a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E8=89=B3=E5=85=B5?= Date: Sat, 18 Apr 2026 21:38:52 +0800 Subject: [PATCH] fix: preserve null unions in setFieldsValue types --- src/interface.ts | 14 ++++++-------- tests/nameTypeCheck.test.tsx | 25 ++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/interface.ts b/src/interface.ts index f18799a8e..1af70ea59 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -245,14 +245,12 @@ export interface InternalHooks { /** Only return partial when type is not any */ type RecursivePartial = - NonNullable extends object - ? { - [P in keyof T]?: NonNullable extends (infer U)[] - ? RecursivePartial[] - : NonNullable extends object - ? RecursivePartial - : T[P]; - } + T extends (infer U)[] + ? RecursivePartial[] + : T extends object + ? { + [P in keyof T]?: RecursivePartial; + } : T; export type FilterFunc = (meta: Meta | null) => boolean; diff --git a/tests/nameTypeCheck.test.tsx b/tests/nameTypeCheck.test.tsx index 9179a9acf..942b565bf 100644 --- a/tests/nameTypeCheck.test.tsx +++ b/tests/nameTypeCheck.test.tsx @@ -2,7 +2,7 @@ import React, { useMemo } from 'react'; import { render } from '@testing-library/react'; import Form, { Field, List } from '../src'; -import type { NamePath } from '../src/interface'; +import type { FormInstance, NamePath } from '../src/interface'; describe('nameTypeCheck', () => { it('typescript', () => { @@ -13,10 +13,33 @@ describe('nameTypeCheck', () => { d?: { d1?: string[]; d2?: string }; e?: { e1?: { e2?: string; e3?: string[]; e4: { e5: { e6: string } } } }; list?: { age?: string }[]; + strictList?: { age: string; name: string }[]; + user?: { profile: { name: string; tags: string[] } }; + nullableList?: string[] | null; + nullableObjectList?: { name?: string }[] | null; }; type fieldType = NamePath; + type SetFieldsValueParam = Parameters['setFieldsValue']>[0]; + + const nullableListAsNull: SetFieldsValueParam = { nullableList: null }; + const nullableListAsArray: SetFieldsValueParam = { nullableList: ['bamboo'] }; + const nullableObjectListAsNull: SetFieldsValueParam = { nullableObjectList: null }; + const nullableObjectListAsArray: SetFieldsValueParam = { + nullableObjectList: [{ name: 'light' }], + }; + const optionalNestedObjectAsPartial: SetFieldsValueParam = { + user: { + profile: { + name: 'light', + }, + }, + }; + const optionalListAsPartial: SetFieldsValueParam = { + strictList: [{ age: '18' }], + }; + const Demo: React.FC = () => { return (