logoAnt Design

⌘ K
  • 디자인
  • 개발
  • 컴포넌트
  • 블로그
  • 자료
5.21.3
  • 为什么禁用日期这么难?
  • Why is it so hard to disable the date?
  • 封装 Form.Item 实现数组转对象
  • HOC Aggregate FieldItem
  • 行省略计算
  • Line Ellipsis Calculation
  • 📢 v4 维护周期截止
  • 📢 v4 surpassed maintenance period
  • Type Util
  • 一个构建的幽灵
  • A build ghost
  • 当 Ant Design 遇上 CSS 变量
  • Ant Design meets CSS Variables
  • API 기술 부채
  • 생동감 있는 Notification
  • 色彩模型与颜色选择器
  • Color Models and Color Picker
  • 主题拓展
  • Extends Theme
  • 虚拟表格来了!
  • Virtual Table is here!
  • Happy Work 테마
  • Happy Work Theme
  • 동적 스타일은 어디로 갔을까?
  • Suspense 引发的样式丢失问题
  • Suspense breaks styles
  • Bundle Size Optimization
  • 안녕, GitHub Actions
  • 所见即所得
  • To be what you see
  • 정적 메서드의 고통
  • SSR에서 정적 스타일 추출
  • SSR Static style export
  • 의존성 문제 해결
  • 贡献者开发维护指南
  • Contributor development maintenance guide
  • 转载-如何提交无法解答的问题
  • Repost: How to submit a riddle
  • 新的 Tooltip 对齐方式
  • Tooltip align update
  • Unnecessary Rerender
  • 如何成长为 Collaborator
  • How to Grow as a Collaborator
  • Funny Modal hook BUG
  • Modal hook 的有趣 BUG
  • about antd test library migration
  • antd 测试库迁移的那些事儿
  • Tree 的勾选传导
  • Tree's check conduction
  • getContainer 的一些变化
  • Some change on getContainer
  • Component-level CSS-in-JS

Type Util

Resources

Ant Design Charts
Ant Design Pro
Ant Design Pro Components
Ant Design Mobile
Ant Design Mini
Ant Design Landing-Landing Templates
Scaffolds-Scaffold Market
Umi-React Application Framework
dumi-Component doc generator
qiankun-Micro-Frontends Framework
ahooks-React Hooks Library
Ant Motion-Motion Solution
China Mirror 🇨🇳

Community

Awesome Ant Design
Medium
Twitter
yuque logoAnt Design in YuQue
Ant Design in Zhihu
Experience Cloud Blog
seeconf logoSEE Conf-Experience Tech Conference
Work with Us

Help

GitHub
Change Log
FAQ
Bug Report
Issues
Discussions
StackOverflow
SegmentFault

Ant XTech logoMore Products

yuque logoYuQue-Document Collaboration Platform
AntV logoAntV-Data Visualization
Egg logoEgg-Enterprise Node.js Framework
Kitchen logoKitchen-Sketch Toolkit
Galacean logoGalacean-Interactive Graphics Solution
xtech logoAnt Financial Experience Tech
Theme Editor
Made with ❤ by
Ant Group and Ant Design Community
loading

TypeScript의 타입 정의는 매우 강력한 도구입니다. 이는 많은 문제를 해결하고 개발자들이 실행 시 발생하는 번거로운 디버깅을 피할 수 있도록 미리 타입 오류를 찾아줍니다. antd에서는 컴포넌트의 기본 정의를 내보내는데, 이는 다음과 같은 코드에서 볼 수 있습니다:

import React from 'react';
import { Table, type TableColumnsType } from 'antd';
const columns: TableColumnsType = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
},
];
export default () => <Table columns={columns} />;

이러한 타입 정의는 대부분의 상황에서 충분하지만, 때로는 더 세밀한 타입 정의가 필요할 수 있습니다. antd는 이러한 모든 것을 내보내지는 않기 때문에, 과거에는 TypeScript의 타입 유추를 활용하여 직접 확장하는 것을 권장했습니다:

import type { SelectProps } from 'antd';
type SelectOption<T> = NonNullable<SelectProps<T>['options']>[number];

TypeScript에 익숙한 개발자에게는 어렵지 않은 작업이지만, 초보자들에게는 다소 어려울 수 있습니다. 이러한 문제를 해결하기 위해 antd는 개발자들이 타입을 쉽게 추출할 수 있도록 도와주는 유틸리티 타입 라이브러리를 만들었습니다.

Type Util

antd는 현재 다음과 같은 3가지 유틸리티 타입을 제공합니다:

  • GetProps<ComponentType>
  • GetProp<ComponentTypeOrComponentPropsType, PropName>
  • GetRef<ComponentType>

앞의 두 타입은 컴포넌트의 props 타입을 쉽게 추출하도록 도와주고, 마지막 타입은 컴포넌트의 ref 타입을 추출하는 데 사용됩니다. 다음 예시를 통해 이러한 타입의 사용법을 이해할 수 있습니다:

GetProps를 통한 속성 정의 가져오기

antd의 일부 자식 컴포넌트는 정의가 내보내지지 않을 수 있습니다. GetProps를 사용하여 직접 가져올 수 있습니다.

import type { Checkbox, GetProps } from 'antd';
type CheckboxGroupType = GetProps<typeof Checkbox.Group>;

GetProp을 통한 속성 타입 가져오기

컴포넌트의 속성 타입은 GetProp을 통해 가져올 수 있습니다. 이 타입은 이미 NonNullable로 감싸져 있기 때문에 null 값에 대한 처리를 고려할 필요가 없습니다:

import type { GetProp, Select, SelectProps } from 'antd';
// 둘 다 동일하게 작동합니다
type SelectOptionType1 = GetProp<SelectProps, 'options'>[number];
type SelectOptionType2 = GetProp<typeof Select, 'options'>[number];

GetRef를 통한 ref 타입 가져오기

GetRef를 사용하면 컴포넌트의 ref 타입을 HTMLElement 또는 특정 정의인지 기억할 필요 없이 간편하게 가져올 수 있습니다. 그냥 사용하면 됩니다:

import React, { forwardRef } from 'react';
import type { GetRef, Select } from 'antd';
type SelectRefType = GetRef<typeof Select>; // BaseSelectRef
const Div = forwardRef<HTMLDivElement>((_, ref) => <div ref={ref} />);
type DomRefType = GetRef<typeof Div>; // HTMLDivElement

마무리

우리가 제공한 타입 유틸리티가 도움이 되길 바랍니다. 더 좋은 아이디어가 있다면 GitHub에 issue나 PR을 제출해주세요.