Is there a way to declare the different types of var id along with its properties in Typescript?

I recently received a task to convert a JavaScript file to a TypeScript file. One issue I am currently facing is whether or not I should define types for the 'id' with this expression, e.g., id={id}.

So far, I have tried:

  1. Even though I defined it as any type, the error underlines persist.
  2. And the error messages...
  • When hovering over its interface 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 the 'var id' error message is:

    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 states:

    (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 is my converted code so far:

import React from 'react'
import { ProfileIcon } from 'components/ProfileIcon'

interface ProfileIconListProps {
  id: string
  size: string
  isLeft: boolean
  profile: Array<Member>
}

**// I suspect this is the problem, but unsure...**
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 onwards, all lines of codes are marked with red error highlights.**
  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 the original JS format:

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

Answer №1

When you wrote the code in TypeScript, the react function expects to receive an object with the key props, but it is actually receiving an object with props keys directly.

To fix this issue, consider updating the function to:

export function ProfileIconList(props: ProfileIconListProps): JSX.Element {

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In React, the edit mode fails to display class attributes

When I attempted to combine both the creation and editing functionalities in one form, I encountered a problem. Specifically, I was able to retrieve the correct ID value when editing an element, but I struggled to access the attribute values. import { yup ...

TypeScript Tutorial: How to retrieve the data type of a deeply nested object

I have a question about extracting the type of a nested object with similar structures. The object in question contains multiple sub-objects that all follow the same pattern. const VALUES = { currentStreak: { display: "Current streak", ...

Is it feasible to establish a direct link between an Angular.js application and Dynamodb? Are there any other solutions available to eliminate the need for a backend

Personally, I believe that removing the backend could be a successful strategy for addressing sysadmin/scale issues. Do you agree with this approach? Is there a proven method for eliminating the backend in web applications that require database access? I& ...

Showing JSON information on a web browser

Here is a snippet of JSON data that I am working with: {"earthquakes":[{"datetime":"2011-03-11 04:46:23","depth":24.39999999999999857891452847979962825775146484375,"lng":142.36899999999999977262632455676794 ...

Using node.js to download files with axios, streaming the content, and then

I am attempting to download a PDF file using axios, and save it on the server side using fs.writeFile. My code is as follows: axios.get('https://xxx/my.pdf', {responseType: 'blob'}).then(response => { fs.writeFile('/temp/my ...

The signature provided by the pusher is invalid: The expected HMAC SHA256 in hexadecimal digest is

The HTML file contains JavaScript code that calls the server for authentication. The code snippet from the HTML file is as follows: <html> <script> <head> var options = { authEndpoint: "api/pusher.json?socket_id=9900&channel_name ...

Inspecting an unspecified generic parameter for absence yields T & ({} | null)

Recently, I came across a perplexing issue while responding to this specific inquiry. It pertains to a scenario where a generic function's parameter is optional and its type involves the use of generics. Consider the following function structure: func ...

A comprehensive guide on implementing filtering in AngularJS arrays

I am working with the array provided below: [ { Id: 1, Name: "AI", Capacity: 2, From: "2021-10-27T08:00:00", To: "2021-10-27T08:50:00", }, { Id: 2, Name: "TEST", Capacity: 2, ...

Is there anybody who can assist me with ${}?

Having an issue with using ${'variable'+i} in a loop function. The goal is to call each function from a loop. Explored template literals but couldn't find a solution for this specific problem. Desired format: ${'variable'+i} // (w ...

Kendo UI Web - MultiSelect: choosing an option multiple times

Currently, I am encountering an issue with the Kendo UI MultiSelect widget when trying to select an option multiple times. An example of this is shown in the image below where I want to choose Schindler's List again after selecting The Dark Knight. Ho ...

Tips for customizing the appearance of the Alert Dialog in React-admin?

React-admin alert dialog example I'm currently working on a React-admin project and I am looking to customize the alert dialog that displays errors, warnings, and success messages on the page. Specifically, I want to apply CSS styles like z-index and ...

Mastering the Art of Unit Testing React Components

In my current project, I am using the testing tools Jest and Enzyme to test a React component. This component relies on a third-party React UI library called Ant Design (Antd). To better illustrate my question, I have provided a simplified version of the c ...

Arrange items in a particular order

I need help sorting the object below by name. The desired order is 1)balloon 2)term 3)instalment. loanAdjustmentList = [ { "description": "Restructure Option", "name": "instalment", " ...

Issue with retrieving the current location while the map is being dragged

How can I retrieve the current latitude and longitude coordinates when the map is dragged? I've tried using the following code: google.maps.event.addListener(map, 'drag', function(event) { addMarker(event.latLng.lat(), event.la ...

What is the best way to organize and group messages in JavaScript in order to push them to a subarray?

Having trouble coming up with a name for this question, but here's the issue I'm facing: I currently have an array in PHP containing several entries. Array ( [0] => stdClass Object ( [sender_id] => 0 [me ...

Unable to modify variable to play audio

As I work on constructing my website, I am incorporating various sounds to enhance user experience when interacting with buttons and other elements. However, managing multiple audio files has proven challenging, as overlapping sounds often result in no aud ...

What sets apart exporting a component from importing its module in another module?

When working with Angular, consider having both module A and module B. If I intend to utilize "A-component" within components of module B, what is the distinction between importing module A in Module B compared to including the "A-component" in the exports ...

Having trouble retrieving the $_SESSION variable accurately from AngularJS

I am working with angularjs and need to retrieve a php session variable. I have created a php file named session.php with the following content: $_SESSION['phone'] = '55551864'; return json_encode($_SESSION['phone']); In my ...

Enhance your HTML rendering with Vue.js directives

Check out this cool code example I created. It's a simple tabs system built using Vue.js. Every tab pulls its content from an array like this: var tabs = [ { title: "Pictures", content: "Pictures content" }, { title: "Music", c ...

Is there a way to achieve this without relying on Ext.getCmp in this particular code snippet?

I am currently using the following code to trigger a button click event once the window show event is called. It works perfectly fine, but I want to achieve the same functionality without using Ext.getCmp. Here is the line of code: Ext.getCmp('recen ...