From 67d3f42c21edb3e837ac2aaf87621d48d2a16744 Mon Sep 17 00:00:00 2001 From: crazyair <645381995@qq.com> Date: Wed, 2 Aug 2023 10:00:57 +0800 Subject: [PATCH 1/3] feat: test --- src/namePathType.ts | 31 ++++++++++++++++++++++++++++--- src/useWatch.ts | 36 +++++------------------------------- tests/useWatch.test.tsx | 24 +++++++++++++++++++++--- 3 files changed, 54 insertions(+), 37 deletions(-) diff --git a/src/namePathType.ts b/src/namePathType.ts index 67a0245f..9df5352a 100644 --- a/src/namePathType.ts +++ b/src/namePathType.ts @@ -13,18 +13,43 @@ export type DeepNamePath< ? ParentNamePath['length'] extends 0 ? Store | BaseNamePath // Return `BaseNamePath` instead of array if `ParentNamePath` is empty : Store extends any[] - ? [...ParentNamePath, number] // Connect path + ? readonly [...ParentNamePath, number] // Connect path : never : Store extends any[] // Check if `Store` is `any[]` ? // Connect path. e.g. { a: { b: string }[] } // Get: [a] | [ a,number] | [ a ,number , b] - [...ParentNamePath, number] | DeepNamePath + readonly [...ParentNamePath, number] | DeepNamePath : { // Convert `Store` to . We mark key a `FieldKey` [FieldKey in keyof Store]: Store[FieldKey] extends Function ? never : | (ParentNamePath['length'] extends 0 ? FieldKey : never) // If `ParentNamePath` is empty, it can use `FieldKey` without array path - | [...ParentNamePath, FieldKey] // Exist `ParentNamePath`, connect it + | readonly [...ParentNamePath, FieldKey] // Exist `ParentNamePath`, connect it | DeepNamePath[FieldKey], [...ParentNamePath, FieldKey]>; // If `Store[FieldKey]` is object }[keyof Store]; + +export type Demo< + T = any, + T1 extends readonly any[] = [], + T2 extends readonly any[] = [], +> = T2['length'] extends T1['length'] ? T : Demo; + +type ddd = DeepNamePath; + +function func = ddd>( + data: T, + params: T1, +): T1 extends readonly any[] ? Demo : Demo; + +function func(...e: any[]) { + return e; +} + +export const d = func({ a: 1, b: '' }, ['a']); + +export const d1 = func({ a: 1, b: '' }, 'a'); + +export const d2 = func({ a: 1, b: '' }, ['b']); + +export const d3 = func({ a: { a1: ['aa'] }, b: '' }, ['a', 'a1', 1]); diff --git a/src/useWatch.ts b/src/useWatch.ts index 23c41bc6..9caf3b6a 100644 --- a/src/useWatch.ts +++ b/src/useWatch.ts @@ -11,6 +11,7 @@ import type { import { useState, useContext, useEffect, useRef, useMemo } from 'react'; import { getNamePath, getValue } from './utils/valueUtil'; import { isFormInstance } from './utils/typeUtil'; +import type { DeepNamePath, Demo } from './namePathType'; type ReturnPromise = T extends Promise ? ValueType : never; type GetGeneric = ReturnPromise>; @@ -37,39 +38,12 @@ const useWatchWarning = : () => {}; function useWatch< - TDependencies1 extends keyof GetGeneric, - TForm extends FormInstance, - TDependencies2 extends keyof GetGeneric[TDependencies1], - TDependencies3 extends keyof GetGeneric[TDependencies1][TDependencies2], - TDependencies4 extends keyof GetGeneric[TDependencies1][TDependencies2][TDependencies3], + TForm extends FormInstance = FormInstance, + const TDependencies extends DeepNamePath> = any, >( - dependencies: [TDependencies1, TDependencies2, TDependencies3, TDependencies4], + dependencies: TDependencies, form?: TForm | WatchOptions, -): GetGeneric[TDependencies1][TDependencies2][TDependencies3][TDependencies4]; - -function useWatch< - TDependencies1 extends keyof GetGeneric, - TForm extends FormInstance, - TDependencies2 extends keyof GetGeneric[TDependencies1], - TDependencies3 extends keyof GetGeneric[TDependencies1][TDependencies2], ->( - dependencies: [TDependencies1, TDependencies2, TDependencies3], - form?: TForm | WatchOptions, -): GetGeneric[TDependencies1][TDependencies2][TDependencies3]; - -function useWatch< - TDependencies1 extends keyof GetGeneric, - TForm extends FormInstance, - TDependencies2 extends keyof GetGeneric[TDependencies1], ->( - dependencies: [TDependencies1, TDependencies2], - form?: TForm | WatchOptions, -): GetGeneric[TDependencies1][TDependencies2]; - -function useWatch, TForm extends FormInstance>( - dependencies: TDependencies | [TDependencies], - form?: TForm | WatchOptions, -): GetGeneric[TDependencies]; +): Demo, TDependencies extends readonly any[] ? TDependencies : [TDependencies]>; function useWatch( dependencies: [], diff --git a/tests/useWatch.test.tsx b/tests/useWatch.test.tsx index 6e22819b..53103121 100644 --- a/tests/useWatch.test.tsx +++ b/tests/useWatch.test.tsx @@ -258,7 +258,7 @@ describe('useWatch', () => { demo?: string; demo2?: string; id?: number; - demo1?: { demo2?: { demo3?: { demo4?: string } } }; + demo1?: { demo2?: { demo3?: { demo4?: { demo5?: { demo6: { demo7?: string } } } } } }; }; const Demo: React.FC = () => { @@ -271,10 +271,28 @@ describe('useWatch', () => { const demo3 = Form.useWatch(['demo1', 'demo2', 'demo3'], form); const demo4 = Form.useWatch(['demo1', 'demo2', 'demo3', 'demo4'], form); const demo5 = Form.useWatch(['demo1', 'demo2', 'demo3', 'demo4', 'demo5'], form); - const more = Form.useWatch(['age', 'name', 'gender'], form); + const demo6 = Form.useWatch(['demo1', 'demo2', 'demo3', 'demo4', 'demo5', 'demo6'], form); + const demo7 = Form.useWatch( + ['demo1', 'demo2', 'demo3', 'demo4', 'demo5', 'demo6', 'demo7'], + form, + ); const demo = Form.useWatch(['demo']); return ( - <>{JSON.stringify({ values, main, age, demo1, demo2, demo3, demo4, demo5, more, demo })} + <> + {JSON.stringify({ + values, + main, + age, + demo1, + demo2, + demo3, + demo4, + demo5, + demo6, + demo7, + demo, + })} + ); }; From 5d6e25d971330ae24b257f61424621457e1206be Mon Sep 17 00:00:00 2001 From: crazyair <645381995@qq.com> Date: Wed, 2 Aug 2023 10:04:00 +0800 Subject: [PATCH 2/3] feat: test --- src/namePathType.ts | 23 ++--------------------- src/useWatch.ts | 7 +++++-- 2 files changed, 7 insertions(+), 23 deletions(-) diff --git a/src/namePathType.ts b/src/namePathType.ts index 9df5352a..964e16c6 100644 --- a/src/namePathType.ts +++ b/src/namePathType.ts @@ -29,27 +29,8 @@ export type DeepNamePath< | DeepNamePath[FieldKey], [...ParentNamePath, FieldKey]>; // If `Store[FieldKey]` is object }[keyof Store]; -export type Demo< +export type GetNameType< T = any, T1 extends readonly any[] = [], T2 extends readonly any[] = [], -> = T2['length'] extends T1['length'] ? T : Demo; - -type ddd = DeepNamePath; - -function func = ddd>( - data: T, - params: T1, -): T1 extends readonly any[] ? Demo : Demo; - -function func(...e: any[]) { - return e; -} - -export const d = func({ a: 1, b: '' }, ['a']); - -export const d1 = func({ a: 1, b: '' }, 'a'); - -export const d2 = func({ a: 1, b: '' }, ['b']); - -export const d3 = func({ a: { a1: ['aa'] }, b: '' }, ['a', 'a1', 1]); +> = T2['length'] extends T1['length'] ? T : GetNameType; diff --git a/src/useWatch.ts b/src/useWatch.ts index 9caf3b6a..6db10fbc 100644 --- a/src/useWatch.ts +++ b/src/useWatch.ts @@ -11,7 +11,7 @@ import type { import { useState, useContext, useEffect, useRef, useMemo } from 'react'; import { getNamePath, getValue } from './utils/valueUtil'; import { isFormInstance } from './utils/typeUtil'; -import type { DeepNamePath, Demo } from './namePathType'; +import type { DeepNamePath, GetNameType } from './namePathType'; type ReturnPromise = T extends Promise ? ValueType : never; type GetGeneric = ReturnPromise>; @@ -43,7 +43,10 @@ function useWatch< >( dependencies: TDependencies, form?: TForm | WatchOptions, -): Demo, TDependencies extends readonly any[] ? TDependencies : [TDependencies]>; +): GetNameType< + GetGeneric, + TDependencies extends readonly any[] ? TDependencies : [TDependencies] +>; function useWatch( dependencies: [], From 3fb88eff587f3af2a9dc9461acf57bfb6e2d688a Mon Sep 17 00:00:00 2001 From: crazyair <645381995@qq.com> Date: Wed, 2 Aug 2023 10:07:13 +0800 Subject: [PATCH 3/3] feat: test --- src/namePathType.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/namePathType.ts b/src/namePathType.ts index 964e16c6..ee6d0022 100644 --- a/src/namePathType.ts +++ b/src/namePathType.ts @@ -30,7 +30,9 @@ export type DeepNamePath< }[keyof Store]; export type GetNameType< - T = any, - T1 extends readonly any[] = [], - T2 extends readonly any[] = [], -> = T2['length'] extends T1['length'] ? T : GetNameType; + Store = any, + NamePath extends readonly any[] = [], + NamePathCache extends readonly any[] = [], +> = NamePathCache['length'] extends NamePath['length'] + ? Store + : GetNameType;