diff --git a/src/interface.ts b/src/interface.ts index f18799a8..1af70ea5 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 9179a9ac..942b565b 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 (