【发布时间】:2021-01-24 05:21:44
【问题描述】:
我正在尝试创建一个自定义“输入”组件并扩展默认的 html 输入属性(道具)。
但是,我也尝试在我自己的接口定义中使用 Typescript 的 Omit 来覆盖默认的“大小”属性,但似乎无法使其正常工作。
(我也尝试过 Typescript 的 Pick/Exclude 和自定义 Override 类型,但得到相同的错误...)
我已经尝试过的类似主题:
- Override the properties of an interface in TypeScript
- How extend React types to support html attributes as props
import React from 'react';
type A = React.HTMLAttributes<HTMLInputElement>;
interface InputProps extends Omit<A, 'size'> {
size?: 'sm' | 'md' | 'lg';
}
const Input = (props: InputProps) => {
return <input {...props} />;
};
TS 错误
(property) JSX.IntrinsicElements.input: React.DetailedHTMLProps<React.InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>
Type '{ size?: "sm" | "md" | "lg"; defaultChecked?: boolean; defaultValue?: string | number | readonly string[]; suppressContentEditableWarning?: boolean; ... 250 more ...; onTransitionEndCapture?: (event: TransitionEvent<...>) => void; }' is not assignable to type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>'.
Type '{ size?: "sm" | "md" | "lg"; defaultChecked?: boolean; defaultValue?: string | number | readonly string[]; suppressContentEditableWarning?: boolean; ... 250 more ...; onTransitionEndCapture?: (event: TransitionEvent<...>) => void; }' is not assignable to type 'InputHTMLAttributes<HTMLInputElement>'.
Types of property 'size' are incompatible.
Type 'string' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.ts(2322)
【问题讨论】:
标签: reactjs typescript