[英]Type 'string' is not assignable to type 'T'. using typescript
I have the next scenario:我有下一个场景:
export type TagType = 'input' | 'textarea';
interface ContainerProps<T extends TagType> {
title: string;
as: T;
className?: string;
}
My component looks like this:我的组件如下所示:
const Container = <T extends TagType>({
title,
as: HTMLEl = 'input',
...rest
}: ContainerProps<T> &
(
| React.TextareaHTMLAttributes<HTMLTextAreaElement>
| React.InputHTMLAttributes<HTMLInputElement>
)) => { ... }
The issue appears as: HTMLEl = 'input',
with the next message:问题显示
as: HTMLEl = 'input',
以及下一条消息:
TS2322: Type '"input"' is not assignable to type 'T'. '"input"' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'ElementType'.
Question: Why this issue appear and how to solve it?问题:为什么会出现这个问题,如何解决?
you must specify the "input" type.您必须指定“输入”类型。
as: HTMLEl = "input" as T,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.