From d11080a47ea345fdd989150cbed7a997d7e03da7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B9=BD=E9=96=92?= <34772477+superfreeeee@users.noreply.github.com> Date: Mon, 29 Jan 2024 17:30:10 +0800 Subject: [PATCH] feat: improve checkers generics (#4075) --- packages/core/src/shared/checkers.ts | 39 +++++++++++++++++++++------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/packages/core/src/shared/checkers.ts b/packages/core/src/shared/checkers.ts index 2a976c40052..fd14a6c196b 100644 --- a/packages/core/src/shared/checkers.ts +++ b/packages/core/src/shared/checkers.ts @@ -1,5 +1,5 @@ import { isFn } from '@formily/shared' -import { DataField } from '..' +import { DataField, JSXComponent } from '..' import { Form, Field, @@ -20,27 +20,48 @@ export const isForm = (node: any): node is Form => { return node instanceof Form } -export const isField = (node: any): node is Field => { - return node instanceof Field -} - export const isGeneralField = (node: any): node is GeneralField => { return node instanceof Field || node instanceof VoidField } -export const isArrayField = (node: any): node is ArrayField => { +export const isField = < + Decorator extends JSXComponent = any, + Component extends JSXComponent = any, + TextType = any, + ValueType = any +>( + node: any +): node is Field => { + return node instanceof Field +} + +export const isArrayField = < + Decorator extends JSXComponent = any, + Component extends JSXComponent = any +>( + node: any +): node is ArrayField => { return node instanceof ArrayField } -export const isObjectField = (node: any): node is ObjectField => { +export const isObjectField = < + Decorator extends JSXComponent = any, + Component extends JSXComponent = any +>( + node: any +): node is ObjectField => { return node instanceof ObjectField } -export const isVoidField = (node: any): node is VoidField => { +export const isVoidField = ( + node: any +): node is VoidField => { return node instanceof VoidField } -export const isFormState = (state: any): state is IFormState => { +export const isFormState = = any>( + state: any +): state is IFormState => { if (isFn(state?.initialize)) return false return state?.displayName === 'Form' }