use icon indicating copy to clipboard operation
use copied to clipboard

Support TS generics

Open reinos opened this issue 5 years ago • 3 comments

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

What problem does this feature solve?

Currently some functions does not support generics so we cannot leverage the power of typescript on returning data.

Take for example this one --> https://2x.antdv.com/components/form/#components-form-demo-useform

I have an interface

interface User {
email: string
firstName: string
lastName: string
}

I want to control the validation to return that type

const data = await validate<User>()

So I am sure that the validate function just return the correct type.

What does the proposed API look like?

Looking in the generated type files I see

declare function useForm(modelRef: Props, rulesRef?: Props, options?: {
immediate?: boolean;
deep?: boolean;
validateOnRuleChange?: boolean;
debounce?: DebounceSettings;
}): {
modelRef: Props;
rulesRef: Props;
initialModel: Props;
validateInfos: validateInfos;
resetFields: () => void;
validate: (names?: string | string[], option?: validateOptions) => Promise<any>;
validateField: (name?: string, value?: any, rules?: [Record<string, unknown>], option?: validateOptions) => Promise<any>;
mergeValidateInfo: (items: validateInfo | validateInfo[]) => validateInfo;
};

Where validate and validateField are set to Promise<any> instead of some thing like

validate: (names?: string | string[], option?: validateOptions)<T = any> => Promise<T>;
validateField: (name?: string, value?: any, rules?: [Record<string, unknown>], option?: validateOptions)<T = any> => Promise<T>;

reinos avatar Oct 09 '20 14:10 reinos

can you pr for it

tangjinzhou avatar Oct 13 '20 01:10 tangjinzhou

@tangjinzhou Just made a PR for this. It only apply to the useForm, as that is what I needed... Perhaps there are more places that are useful for generics.

reinos avatar Oct 13 '20 07:10 reinos

Thanks for merging 😄 Can you also bump up the version so it become available?

reinos avatar Oct 23 '20 06:10 reinos