Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Support TS generics #19

Open
1 task done
reinos opened this issue Oct 9, 2020 · 3 comments
Open
1 task done

Support TS generics #19

reinos opened this issue Oct 9, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@reinos
Copy link
Contributor

reinos commented Oct 9, 2020

  • 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 reinos changed the title Support generics Support TS generics Oct 9, 2020
@tangjinzhou tangjinzhou transferred this issue from vueComponent/ant-design-vue Oct 13, 2020
@tangjinzhou tangjinzhou added the enhancement New feature or request label Oct 13, 2020
@tangjinzhou
Copy link
Member

tangjinzhou commented Oct 13, 2020

can you pr for it

@reinos
Copy link
Contributor Author

reinos commented Oct 13, 2020

@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
Copy link
Contributor Author

reinos commented Oct 23, 2020

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants