TypeScript material-ui 允许未在@types 中定义的道具
TypeScript material-ui allow props not defined in @types
我有一个使用 React 的设置,material-ui,Flow,Jest 在我的测试中做快照。
为了创建一致的快照,我需要在我的 material-ui 组件中定义 ID,否则它们是自动生成的并且每次都不同。
所以我这样做了:
<Card style = { style } id = { id ?
${ id }-card: null }>
这很好用。现在我正在切换到 TypeScript,并且有 @types/material-ui。打字稿抱怨 id 道具:
[ts] Property 'id' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Card> ...
我做错了什么吗?有没有办法抑制这种情况?我知道该组件支持传递 id。
查看source code for that definition,确实不支持id
属性。
如果您认为这是一个错误,我会向 DefinitelyTyped 提交拉取请求!
我最终创建了 typings/material-ui/index.d.ts
:
declare namespace __MaterialUI
{
interface SelectFieldProps
{
multiple?: boolean;
selectionRenderer?: (values: any[]) => string;
}
namespace Card
{
interface CardProps
{
id?: string;
}
interface CardHeaderProps
{
id?: string;
}
interface CardTitleProps
{
id?: string;
}
interface CardActionsProps
{
id?: string;
}
interface CardTextProps
{
id?: string;
}
}
}
和 tsconfig:
"include": [
"./components/**/*",
"./typings/**/*"
],
这解决了我的问题
我有一个使用 React 的设置,material-ui,Flow,Jest 在我的测试中做快照。
为了创建一致的快照,我需要在我的 material-ui 组件中定义 ID,否则它们是自动生成的并且每次都不同。
所以我这样做了:
<Card style = { style } id = { id ?
${ id }-card: null }>
这很好用。现在我正在切换到 TypeScript,并且有 @types/material-ui。打字稿抱怨 id 道具:
[ts] Property 'id' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Card> ...
我做错了什么吗?有没有办法抑制这种情况?我知道该组件支持传递 id。
查看source code for that definition,确实不支持id
属性。
如果您认为这是一个错误,我会向 DefinitelyTyped 提交拉取请求!
我最终创建了 typings/material-ui/index.d.ts
:
declare namespace __MaterialUI
{
interface SelectFieldProps
{
multiple?: boolean;
selectionRenderer?: (values: any[]) => string;
}
namespace Card
{
interface CardProps
{
id?: string;
}
interface CardHeaderProps
{
id?: string;
}
interface CardTitleProps
{
id?: string;
}
interface CardActionsProps
{
id?: string;
}
interface CardTextProps
{
id?: string;
}
}
}
和 tsconfig:
"include": [
"./components/**/*",
"./typings/**/*"
],
这解决了我的问题