Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,11 @@ export interface InternalHooks {
getInitialValue: (namePath: InternalNamePath) => StoreValue;
}

type Primitive = string | number | bigint | boolean | symbol | null | undefined;

/** Only return partial when type is not any */
export type RecursivePartial<T> = T extends Date | RegExp | Function | Map<any, any> | Set<any>
export type RecursivePartial<T> = T extends
Primitive | Date | RegExp | Function | Map<any, any> | Set<any>
? T
: T extends (infer U)[]
? RecursivePartial<U>[]
Expand Down
15 changes: 14 additions & 1 deletion tests/nameTypeCheck.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import React, { useMemo } from 'react';
import { render } from '@testing-library/react';
import Form, { Field, List } from '../src';
import type { FormInstance, NamePath } from '../src/interface';
import type { FormInstance, NamePath, RecursivePartial } from '../src/interface';

type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
type Expect<T extends true> = T;

describe('nameTypeCheck', () => {
it('typescript', () => {
Expand All @@ -23,6 +27,11 @@ describe('nameTypeCheck', () => {

type SetFieldsValueParam = Parameters<FormInstance<FieldType>['setFieldsValue']>[0];

type DomainId = string & {
readonly __nominal: unique symbol;
};
type BrandedPartial = RecursivePartial<{ id: DomainId }>;

const nullableListAsNull: SetFieldsValueParam = { nullableList: null };
const nullableListAsArray: SetFieldsValueParam = { nullableList: ['bamboo'] };
const nullableObjectListAsNull: SetFieldsValueParam = { nullableObjectList: null };
Expand All @@ -39,6 +48,10 @@ describe('nameTypeCheck', () => {
const optionalListAsPartial: SetFieldsValueParam = {
strictList: [{ age: '18' }],
};
const brandedValue: BrandedPartial = {
id: 'domain-id' as DomainId,
};
const brandedTypeCheck: Expect<Equal<BrandedPartial['id'], DomainId | undefined>> = true;
const useGenericSetter = <Values,>() => {
const [form] = Form.useForm<Values>();
return (values: Partial<Values>) => form.setFieldsValue(values);
Expand Down
Loading