javascript - TypeScriptReact. ERROR Type '{ key: number; }' is not assignable to type ILink (SFC Component) - St

So, I have a basic iterable Component Link that render some link. He is wrapped up in Links ponent that

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
Add a ment  | 

3 Answers 3

Reset to default 2

Try 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 }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745539784a4632073.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信