我正在尝试创建一个自定义的“输入”组件并扩展默认的 html 输入属性(props)。
但是,我也试图在我自己的接口定义中使用 Typescript 的 Omit 覆盖默认的“大小”属性,但似乎无法让它工作。
(我也试过 Typescript 的 Pick/Exclude 和自定义 Override 类型,但得到同样的错误......)
我已经尝试过类似的线程:
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} />;
};
传输错误
(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)