The definition of TypeScript is very powerful, it can solve many problems, help developers find type errors in advance to avoid painful debugging at runtime. In antd, we also export the basic definitions of components:
importReactfrom'react';
import{Table,typeTableColumnsType}from'antd';
const columns:TableColumnsType=[
{
title:'Name',
dataIndex:'name',
key:'name',
},
];
exportdefault()=><Tablecolumns={columns}/>;
These definitions meet most scenarios, but sometimes developers want to get more refined type definitions, which antd may not export. In the past, we recommended developers to extend them by themselves through TypeScript's type gymnastics to meet their needs:
It's not a difficult task for developer who are familiar with TypeScript. But for TypeScript beginners, this may be a difficult problem. Therefore, we have launched a type tool library to help developers simplify the process of extracting types.
Type Util
We now provide 3 additional utility types in antd:
Previous two are used to help developers extract the props type of the component, and the last one is used to extract the ref type of the component. We can understand the usage of these types through the following examples:
Get props definition by GetProps
Some sub-component definition may not be exported in antd. You can get it directly through GetProps:
For the property type of the component, we can get it through GetProp. It has been encapsulated with NonNullable. So there is no need to consider the null case: