简体   繁体   English

如何使用 Typescript 中的属性定义 var id 的类型?

[英]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:到目前为止,我尝试过:

  1. despite I actually defined it as any type, these error underlines never disappear.尽管我实际上将它定义为任何类型,但这些错误下划线永远不会消失。
  2. And error messages...和错误信息...
  • 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.

相关问题 如何在 Vue 中以 Typescript 语法定义使用 prop 作为其初始值的本地数据属性? - How can I, in Vue, define a local data property that uses a prop as its initial value, in Typescript syntax? 如何在Typescript中定义对象变量的类型? - How can I define the types of an object variable in Typescript? Typescript - 如何根据另一个属性有条件地定义类型 - Typescript - How can I conditionally define a type based on another property 如何在一个var中定义2个类? - How can I define 2 classes in one var? 使用 TypeScript 时,如何使用定义文件定义数组中使用的类型? - How can I define the types used in an array with a definition file when using TypeScript? 如何使用ReactJS和Typescript定义组件实例属性? 属性“ var”在类型“ App”上不存在 - How to define component instance attributes with ReactJS and Typescript? `Property 'var' does not exist on type 'App'` 如何通过var选择JavaScript“ Id”? - How can I select JavaScript “Id” by var? 如何在打字稿中定义jQuery对象? - How can I define a jQuery object in typescript? 使用映射类型时,如何在TypeScript中推断对象属性的值? - How can I infer an object property's value in TypeScript when using mapped types? TypeScript如何在编译时为JavaScript定义内置类型 - How TypeScript can defines Build-In Types to JavaScript on its compilation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM