Day19 在react项目中使用tsReact TypeScript
在@types/react 与 @types/react-dom两个包中可以查看type定义
值空间与类型空间是隔离的,不需要担心循环引用。比如在父组件中定义了内部使用的类型,也可以在子组件中使用import导入,不必担心循环引用问题。
ComponentProps
用于提取元素中的所有属性。
// 基于html原生元素开发时,支持传入原生元素的属性,不用一一声明
// 遇到一些没有导出属性类型定义的也可以使用ComponentProps
import {ComponentProps} from 'react'
interface MyButtonProps extends ComponentProps<'button'> {
name: string
action: string
}
// 类似的还有ComponentPropsWithoutRef、ComponentPropsWithRef