React Typescript-添加自定义属性 [英] React Typescript - Adding custom attribute

查看:992
本文介绍了React Typescript-添加自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

React Typescript允许添加自定义data- *属性。
但是可以添加自定义属性,例如名称 ||。 测试行为。 ?

React Typescript allow to add custom data-* attributes. But is it possible to add custom attributes like 'name' || 'test' act. ?

<span name="I'm causing a type error" data-test="I'm Working"/>

我加粗了。


类型错误:类型'{子代:元素;名称:字符串;数据测试:
字符串; }不能分配为键入
DetailedHTMLProps,HTMLSpanElement>。
属性名称在类型上不存在
DetailedHTMLProps,HTMLSpanElement>。
TS232

type error: Type '{ children: Element; name: string; data-test: string; }' is not assignable to type 'DetailedHTMLProps, HTMLSpanElement>'. Property 'name' does not exist on type 'DetailedHTMLProps, HTMLSpanElement>'. TS232



"react": "^16.7.0",
"typescript": "^3.2.4",


推荐答案

在16以上的反应中,有可能请参阅

in react 16+ it is possible, see

问题是打字稿不知道(还)

probem is that typescript didnt know about it(yet)

但是您仍然可以添加@ts忽略类型检查

but you can still add @ts ignore for typechecking

{ //@ts-ignore
  <span name="I'm causing a type error" data-test="I'm Working"/>
}

这篇关于React Typescript-添加自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆