admin管理员组文章数量:1023242
So, I have a basic iterable Component Link
that render some link. He is wrapped up in Links
ponent that iterate him. Unfortunately, the problem that I faced is the Typescript
does not understand what to do with key
prop on Link ponent
over Links
Component iteration.
I'll gradeful for any help!
Error message:
Type '{ key: number; }' is not assignable to type 'ILink'.
Property 'title' is missing in type '{ key: number; }'.
My code:
import React from 'react'
import styles from './index.cssmodule.scss'
interface ILinks {
links: object[]
}
interface ILink {
title: string
label: string
href: string
icon: string
}
// Link Component
const Link: React.SFC<ILink> = ({ title, label, href, icon }) => (
<a href={href + label}>
<i className={icon} />
<span>{title}</span>
</a>
)
// Links Component that wraps Link
const Links: React.SFC<ILinks> = ({ links = [] }) => {
const bulidLinksList = (): JSX.Element[] => (
links.map((link, i) => (
<Link key={i} {...link} /> // ERROR Type '{ key: number; }' is not assignable to type ILink
))
)
return (
<div className={styles.linksContainer}>
{bulidLinksList()}
</div>
)
}
export default Links
So, I have a basic iterable Component Link
that render some link. He is wrapped up in Links
ponent that iterate him. Unfortunately, the problem that I faced is the Typescript
does not understand what to do with key
prop on Link ponent
over Links
Component iteration.
I'll gradeful for any help!
Error message:
Type '{ key: number; }' is not assignable to type 'ILink'.
Property 'title' is missing in type '{ key: number; }'.
My code:
import React from 'react'
import styles from './index.cssmodule.scss'
interface ILinks {
links: object[]
}
interface ILink {
title: string
label: string
href: string
icon: string
}
// Link Component
const Link: React.SFC<ILink> = ({ title, label, href, icon }) => (
<a href={href + label}>
<i className={icon} />
<span>{title}</span>
</a>
)
// Links Component that wraps Link
const Links: React.SFC<ILinks> = ({ links = [] }) => {
const bulidLinksList = (): JSX.Element[] => (
links.map((link, i) => (
<Link key={i} {...link} /> // ERROR Type '{ key: number; }' is not assignable to type ILink
))
)
return (
<div className={styles.linksContainer}>
{bulidLinksList()}
</div>
)
}
export default Links
Share
Improve this question
edited Jun 20, 2020 at 9:12
CommunityBot
11 silver badge
asked Oct 10, 2018 at 9:22
Max TravisMax Travis
1,3284 gold badges22 silver badges43 bronze badges
1
- emmm,React may think see the key as a property that should pass to Link.I once face a similar situation, maybe not a good way ,but I add a <div key={i}></div>. – Root Commented Oct 10, 2018 at 10:16
3 Answers
Reset to default 2Try using React.ReactElement
instead of React.SFC
, ReactElement
will give you key
and ref
props
const Link: React.ReactElement<ILink> = ({ title, label, href, icon }) => (
<a href={href + label}>
<i className={icon} />
<span>{title}</span>
</a>
)
// Links Component that wraps Link
const Links: React.ReactElement<ILinks> = ({ links = [] }) => {
const bulidLinksList = (): JSX.Element[] => (
links.map((link, i) => (
<Link key={i} {...link} />
))
)
return (
<div className={styles.linksContainer}>
{bulidLinksList()}
</div>
)
}
You are defining an interface Link
like that:
interface ILink {
title: string
label: string
href: string
icon: string
}
Then you try to assign key={i}
, which ends up just being an object like
{
key: 123;
}
You have not defined key
in your ILink
interface. That's why TypeScript plains.
The basic problem here is that on line your are not using ReactElement, but function functional interface React.SFC which will produce ReactElement. That fuction expects interface which you defined like this interface ILink { title: string label: string href: string icon: string }
So, I have a basic iterable Component Link
that render some link. He is wrapped up in Links
ponent that iterate him. Unfortunately, the problem that I faced is the Typescript
does not understand what to do with key
prop on Link ponent
over Links
Component iteration.
I'll gradeful for any help!
Error message:
Type '{ key: number; }' is not assignable to type 'ILink'.
Property 'title' is missing in type '{ key: number; }'.
My code:
import React from 'react'
import styles from './index.cssmodule.scss'
interface ILinks {
links: object[]
}
interface ILink {
title: string
label: string
href: string
icon: string
}
// Link Component
const Link: React.SFC<ILink> = ({ title, label, href, icon }) => (
<a href={href + label}>
<i className={icon} />
<span>{title}</span>
</a>
)
// Links Component that wraps Link
const Links: React.SFC<ILinks> = ({ links = [] }) => {
const bulidLinksList = (): JSX.Element[] => (
links.map((link, i) => (
<Link key={i} {...link} /> // ERROR Type '{ key: number; }' is not assignable to type ILink
))
)
return (
<div className={styles.linksContainer}>
{bulidLinksList()}
</div>
)
}
export default Links
So, I have a basic iterable Component Link
that render some link. He is wrapped up in Links
ponent that iterate him. Unfortunately, the problem that I faced is the Typescript
does not understand what to do with key
prop on Link ponent
over Links
Component iteration.
I'll gradeful for any help!
Error message:
Type '{ key: number; }' is not assignable to type 'ILink'.
Property 'title' is missing in type '{ key: number; }'.
My code:
import React from 'react'
import styles from './index.cssmodule.scss'
interface ILinks {
links: object[]
}
interface ILink {
title: string
label: string
href: string
icon: string
}
// Link Component
const Link: React.SFC<ILink> = ({ title, label, href, icon }) => (
<a href={href + label}>
<i className={icon} />
<span>{title}</span>
</a>
)
// Links Component that wraps Link
const Links: React.SFC<ILinks> = ({ links = [] }) => {
const bulidLinksList = (): JSX.Element[] => (
links.map((link, i) => (
<Link key={i} {...link} /> // ERROR Type '{ key: number; }' is not assignable to type ILink
))
)
return (
<div className={styles.linksContainer}>
{bulidLinksList()}
</div>
)
}
export default Links
Share
Improve this question
edited Jun 20, 2020 at 9:12
CommunityBot
11 silver badge
asked Oct 10, 2018 at 9:22
Max TravisMax Travis
1,3284 gold badges22 silver badges43 bronze badges
1
- emmm,React may think see the key as a property that should pass to Link.I once face a similar situation, maybe not a good way ,but I add a <div key={i}></div>. – Root Commented Oct 10, 2018 at 10:16
3 Answers
Reset to default 2Try using React.ReactElement
instead of React.SFC
, ReactElement
will give you key
and ref
props
const Link: React.ReactElement<ILink> = ({ title, label, href, icon }) => (
<a href={href + label}>
<i className={icon} />
<span>{title}</span>
</a>
)
// Links Component that wraps Link
const Links: React.ReactElement<ILinks> = ({ links = [] }) => {
const bulidLinksList = (): JSX.Element[] => (
links.map((link, i) => (
<Link key={i} {...link} />
))
)
return (
<div className={styles.linksContainer}>
{bulidLinksList()}
</div>
)
}
You are defining an interface Link
like that:
interface ILink {
title: string
label: string
href: string
icon: string
}
Then you try to assign key={i}
, which ends up just being an object like
{
key: 123;
}
You have not defined key
in your ILink
interface. That's why TypeScript plains.
The basic problem here is that on line your are not using ReactElement, but function functional interface React.SFC which will produce ReactElement. That fuction expects interface which you defined like this interface ILink { title: string label: string href: string icon: string }
本文标签:
版权声明:本文标题:javascript - TypeScriptReact. ERROR Type '{ key: number; }' is not assignable to type ILink (SFC Component) - St 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745539783a2155128.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论