[英]How can I define types of var id with its property in Typescript?
I got a job to convert js file to ts file at the moment.我现在有一份将 js 文件转换为 ts 文件的工作。 The issue I encountered now is;
我现在遇到的问题是; have no clue whether I should define types for id with this expression e,g, id={id}
不知道我是否应该用这个表达式为 id 定义类型,例如,id={id}
So far, I attempted:到目前为止,我尝试过:
When I put mouse on its interface's name,当我将鼠标放在其界面名称上时,
Type '{ id: string; }' is missing the following properties from type 'ReactElement<any, any>': type, props, keyts(2739) Conversion of type 'string' to type 'ProfileIconListStyle' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.ts(2352)
and var id error message is:和 var id 错误消息是:
Type '{ id: string; }' is missing the following properties from type 'ReactElement<any, any>': type, props, keyts(2739) Conversion of type 'string' to type 'ProfileIconListStyle' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.ts(2352) '>' expected.ts(1005)
lastly, the property {id} error message is:最后,属性 {id} 错误消息是:
(property) id: string Type '{ id: string; }' is missing the following properties from type 'ReactElement<any, any>': type, props, keyts(2739) Type '{ id: string; }' is not assignable to type 'ProfileIconListStyle'. Object literal may only specify known properties, and 'id' does not exist in type 'ProfileIconListStyle'.ts(2322)
Here are the codes that I have converted it so far.这是我到目前为止转换的代码。
import React from 'react'
import { ProfileIcon } from 'components/ProfileIcon'
interface ProfileIconListProps {
id: string
size: string
isLeft: boolean
profile: Array<Member>
}
**// I believe this is the problem, but no idea... **
interface ProfileIconListStyle {
isLeft: boolean
}
interface Member {
src: string
color: string
}
export function ProfileIconList({ props }: { props: ProfileIconListProps }): JSX.Element {
var { id, size, isLeft, profile } = props
isLeft = isLeft === false ? false : true
**// from here to end, all lines of codes are underlined with red error marks.**
return (
<ProfileIconListStyle id={id} isLeft={isLeft}>
{profile.map((member: Member, index: number) => {
return <ProfileIcon key={`${id}-list-${index}`} props={{ size, src: member.src, color: member.color }} />
})}
</ProfileIconListStyle>
)
}
And origin, js format.和origin,js格式。
import React from 'react'
import { ProfileIcon } from 'components/ProfileIcon'
export function ProfileIconList(props) {
var { id, size, isLeft, profile } = props
isLeft = isLeft === false ? false : true
return (
<ProfileIconListStyle id={id} isLeft={isLeft}>
{profile.map((member, index) => {
return <ProfileIcon key={`${id}-list-${index}`} size={size} src={member.src} color={member.color} />
})}
</ProfileIconListStyle>
)
}
In what you wrote it in TS, the react function is expecting to receive an object that contains the key props
, while it is receiving an object that contains the props keys directly.在您在 TS 中编写的内容中,反应 function 期望收到包含密钥
props
的 object ,而它正在接收直接包含 props 密钥的 object 。
Try changing the function to:尝试将 function 更改为:
export function ProfileIconList(props: ProfileIconListProps): JSX.Element {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.