Skip to content

Commit c35e335

Browse files
authored
Compatible base namePath (#607)
* feat: test * feat: test
1 parent d38ef7f commit c35e335

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/namePathType.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
type BaseNamePath = string | number | boolean | (string | number | boolean)[];
12
/**
23
* Store: The store type from `FormInstance<Store>`
34
* ParentNamePath: Auto generate by nest logic. Do not fill manually.
@@ -8,14 +9,12 @@ export type DeepNamePath<
89
> = ParentNamePath['length'] extends 10
910
? never
1011
: // Follow code is batch check if `Store` is base type
11-
true extends (Store extends string | number | boolean ? true : false)
12+
true extends (Store extends BaseNamePath ? true : false)
1213
? ParentNamePath['length'] extends 0
13-
? Store // Return `string | number | boolean` instead of array if `ParentNamePath` is empty
14+
? Store | BaseNamePath // Return `BaseNamePath` instead of array if `ParentNamePath` is empty
15+
: Store extends any[]
16+
? [...ParentNamePath, number] // Connect path
1417
: never
15-
: true extends (Store extends (string | number | boolean)[] ? true : false)
16-
? ParentNamePath['length'] extends 0
17-
? Store // Return `(string | number | boolean)[]` instead of array if `ParentNamePath` is empty
18-
: [...ParentNamePath, number] // Connect path
1918
: Store extends any[] // Check if `Store` is `any[]`
2019
? // Connect path. e.g. { a: { b: string }[] }
2120
// Get: [a] | [ a,number] | [ a ,number , b]

tests/nameTypeCheck.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
2-
import React from 'react';
2+
import React, { useMemo } from 'react';
33
import { render } from '@testing-library/react';
44
import Form, { Field, List } from '../src';
55
import type { NamePath } from '../src/interface';
@@ -20,6 +20,8 @@ describe('nameTypeCheck', () => {
2020
const Demo: React.FC = () => {
2121
return (
2222
<Form>
23+
<Field name={'use by strictNullChecks set true' as string} />
24+
<Field name={'use by strictNullChecks set true' as string | number} />
2325
{/* 无类型 */}
2426
<Field name={[]} />
2527
<Field name={'a'} />

0 commit comments

Comments
 (0)