chore(TS): add comments to TS type methods (#49224)

This commit is contained in:
2024-06-04 21:00:20 +08:00 committed by GitHub
parent 526d8ba0c2
commit e7d7a0b3cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7,6 +7,17 @@ export type AnyObject = Record<PropertyKey, any>;
export type CustomComponent<P = AnyObject> = React.ComponentType<P> | string; export type CustomComponent<P = AnyObject> = React.ComponentType<P> | string;
/**
* Get component props
* @example
* ```ts
* import { Checkbox } from 'antd'
* import type { GetProps } from 'antd';
*
* type CheckboxGroupProps = GetProps<typeof Checkbox.Group>
* ```
* @since 5.13.0
*/
export type GetProps<T extends React.ComponentType<any> | object> = T extends React.ComponentType< export type GetProps<T extends React.ComponentType<any> | object> = T extends React.ComponentType<
infer P infer P
> >
@ -15,6 +26,23 @@ export type GetProps<T extends React.ComponentType<any> | object> = T extends Re
? T ? T
: never; : never;
/**
* Get component props by component name
* @example
* ```ts
* import { Select } from 'antd';
* import type { GetProp, SelectProps } from 'antd';
*
* type SelectOption1 = GetProp<SelectProps, 'options'>[number];
* // or
* type SelectOption2 = GetProp<typeof Select, 'options'>[number];
*
* const onChange: GetProp<typeof Select, 'onChange'> = (value, option) => {
* // Do something
* };
* ```
* @since 5.13.0
*/
export type GetProp< export type GetProp<
T extends React.ComponentType<any> | object, T extends React.ComponentType<any> | object,
PropName extends keyof GetProps<T>, PropName extends keyof GetProps<T>,
@ -26,6 +54,17 @@ type ReactRefComponent<Props extends { ref?: React.Ref<any> | string }> = (
type ExtractRefAttributesRef<T> = T extends React.RefAttributes<infer P> ? P : never; type ExtractRefAttributesRef<T> = T extends React.RefAttributes<infer P> ? P : never;
/**
* Get component ref
* @example
* ```ts
* import { Input } from 'antd';
* import type { GetRef } from 'antd';
*
* type InputRef = GetRef<typeof Input>;
* ```
* @since 5.13.0
*/
export type GetRef<T extends ReactRefComponent<any> | React.Component<any>> = export type GetRef<T extends ReactRefComponent<any> | React.Component<any>> =
T extends React.Component<any> T extends React.Component<any>
? T ? T