2024-03-21 15:55:40 +08:00
|
|
|
---
|
|
|
|
category: Components
|
|
|
|
title: Util
|
|
|
|
subtitle: 工具类
|
2024-03-22 14:22:42 +08:00
|
|
|
description: 辅助开发,提供一些常用的工具方法。
|
|
|
|
showImport: false
|
2024-03-21 15:55:40 +08:00
|
|
|
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*rRDlT7ST8DUAAAAAAAAAAAAADrJ8AQ/original
|
|
|
|
coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*rRDlT7ST8DUAAAAAAAAAAAAADrJ8AQ/original
|
2024-04-02 14:05:03 +08:00
|
|
|
tag: 5.13.0
|
2024-03-21 15:55:40 +08:00
|
|
|
demo:
|
|
|
|
cols: 2
|
|
|
|
group:
|
|
|
|
title: 其他
|
|
|
|
order: 99
|
|
|
|
---
|
|
|
|
|
2024-04-02 14:05:03 +08:00
|
|
|
自 `5.13.0` 版本开始提供这些方法。
|
|
|
|
|
2024-03-21 15:55:40 +08:00
|
|
|
## GetRef
|
|
|
|
|
|
|
|
获取组件的 `ref` 属性定义,这对于未直接暴露或者子组件的 `ref` 属性定义非常有用。
|
|
|
|
|
|
|
|
```tsx
|
2024-04-02 14:05:03 +08:00
|
|
|
import { Select } from 'antd';
|
|
|
|
import type { GetRef } from 'antd';
|
2024-03-21 15:55:40 +08:00
|
|
|
|
|
|
|
type SelectRefType = GetRef<typeof Select>; // BaseSelectRef
|
|
|
|
```
|
|
|
|
|
|
|
|
## GetProps
|
|
|
|
|
|
|
|
获取组件的 `props` 属性定义:
|
|
|
|
|
|
|
|
```tsx
|
2024-04-02 14:05:03 +08:00
|
|
|
import { Checkbox } from 'antd';
|
|
|
|
import type { GetProps } from 'antd';
|
2024-03-21 15:55:40 +08:00
|
|
|
|
|
|
|
type CheckboxGroupType = GetProps<typeof Checkbox.Group>;
|
|
|
|
```
|
|
|
|
|
|
|
|
## GetProp
|
|
|
|
|
2024-08-05 12:50:19 +08:00
|
|
|
获取组件的单个 `props` 属性定义。它已经将 `NonNullable` 进行了封装,所以不用再考虑为空的情况:
|
2024-03-21 15:55:40 +08:00
|
|
|
|
|
|
|
```tsx
|
2024-04-02 14:05:03 +08:00
|
|
|
import { Select } from 'antd';
|
|
|
|
import type { GetProp, SelectProps } from 'antd';
|
2024-03-21 15:55:40 +08:00
|
|
|
|
|
|
|
// 以下两种都可以生效
|
|
|
|
type SelectOptionType1 = GetProp<SelectProps, 'options'>[number];
|
|
|
|
type SelectOptionType2 = GetProp<typeof Select, 'options'>[number];
|
|
|
|
```
|