![](/img/trans.png)
[英]React/Typescript Property 'data' does not exist on type 'IntrinsicAttributes & [Interface]'
[英]Property does not exist on interface in React Typescript
我是 React 的新手,但之前有过一些 Angular 尝试。
我实际上是在尝试实现一个接受数据模型或对象作为参数的组件
看起来像这样
import react from 'react'
interface SmbListItem{
index:number;
primary:string;
secondary:string;
}
export default function SmbList({props}:SmbListItem){
return(
<>
</>
);
}
但是它的 saying 道具在 SmbListItem 上不存在
我做错了什么?
在这种情况下,还要求对接口或类型提出建议是最好的选择
import react from 'react'
interface SmbListItem{
index:number;
primary:string;
secondary:string;
}
export default function SmbList({index, primary, secondary}:SmbListItem){
return(
<>
</>
);
}
您的Interface Props
中没有定义此类props
即使你不使用Ts
并且你确定你props
像这样destructure
你的props
export default function SmbList({index, primary, secondary}){
return(
<></>
);
}
对于您的第二个问题,请查看类型与接口
首先,您试图destructure
函数定义中的props
参数,但您没有将任何道具传递给函数。 在这种情况下,您需要在这种情况下使用generic type
函数。
如果你是react-typescirpt
世界的新手。 然后,您需要先阅读此内容:- TypeScript Docs 。
泛型类型是一种允许您使用一个或多个占位符类型定义类型或函数的类型,这些占位符类型在使用泛型时可以用特定类型填充。
我找到了一个很好的例子:-
function identity<T>(arg: T): T {
return arg;
}
let numberIdentity = identity(5); // numberIdentity is of type number
let stringIdentity = identity("hello"); // stringIdentity is of type string
这是您的代码示例:-
export default function SmbList<T extends SmbListItem>({ props }: T) {
return <>// You code...</>;
}
props
参数属于扩展SmbListItem
接口的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.