简体   繁体   English

类型“字符串”不可分配给类型“T”。 使用 typescript

[英]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.

相关问题 打字稿:类型“字符串”不可分配给类型 - Typescript: Type 'string' is not assignable to type Typescript 字符串枚举类型 'string' 不可分配给类型 - Typescript String enum type 'string' is not assignable to type Typescript React - 类型“字符串”不可分配给类型“外观” - Typescript React - Type 'string' is not assignable to type 'Appearance' 输入'字符串| 日期'不可分配给类型'ReactNode'.. Typescript - Type 'string | Date' is not assignable to type 'ReactNode'.. Typescript 打字稿:类型“字符串”不可分配给自定义类型 - Typescript: Type 'string' is not assignable to custom type TypeScript 错误:类型“字符串”不可分配给排版类型 - TypeScript error: Type 'string' is not assignable to type for Typography React TypeScript:类型“string []”不可分配给类型“never []” - React TypeScript: Type 'string[]' is not assignable to type 'never[]' 类型“string[]”不可分配给类型“never[]”。 Typescript 错误 - Type 'string[]' is not assignable to type 'never[]'. Typescript Error 打字稿编译错误:类型“ {}”不可分配给类型“ T” - Typescript Compile Error: Type '{}' is not assignable to type 'T' TypeScript:“字符串| 未定义”不能分配给“字符串”类型 - TypeScript: 'string | undefined' is not assignable to type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM