Solid-js component type definitions in Typescript

Is there a way to convert the initial example provided in the Solid-JS documentation to valid TypeScript?

import { render } from "solid-js/web"

const HelloMessage = (props: { name: string }) => <div>Hello {props.name}</div>

render(() => <HelloMessage name="Taylor" />, document.getElementById("hello-example"))

I'm encountering an error related to props lacking type hint, specifically

Parameter 'props' implicitly has an 'any' type.

In React, I typically use React.FC, but I haven't been able to find an equivalent in Solid-JS. Is there another approach I should take?

Answer №1

After looking into it further, I discovered that the solution involves using the Component in a generic way:

import {render} from 'solid-js/web'
import {Component} from 'solid-js'

const Greeting: Component<{name: string}> = (props) => <div>Hello {props.name}</div>

render(() => <Greeting name="Taylor" />, document.getElementById("hello-example"))

Answer №2

An alternative approach could be implemented (without the need to import type Component):

» Check out the Demo

import {render} from 'solid-js/web'

function HelloMessage(props: {
  name: string
}) {
  return (
    <div>
      Hello {props.name}
    </div>
  )
}

render(() => <HelloMessage name="Taylor" />, document.getElementById('app'))

Alternatively, you can consider this concise version:

const HelloMessage = (props: { name: string }) =>
  <div>Hello {props.name}</div> 

By the way, in your question you are using Hello {props.name}, which is correct as props is a reactive object and props.name is also reactive. However, in your answer, you use Hello {name} instead, which will not update dynamically as name is just a constant string and non-reactive by default.

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

"Optimize Your Data with PrimeNG's Table Filtering Feature

I'm currently working on implementing a filter table using PrimeNG, but I'm facing an issue with the JSON structure I receive, which has multiple nested levels. Here's an example: { "id": "123", "category": "nice", "place": { "ran ...

What is the method to activate a chosen cell within an angular table?

When a cell is clicked, it should be the only open and focused cell. If another cell is clicked, the previous one should close. Currently, multiple sunsetDate cells can be open at the same time, which is incorrect. How can this be handled in angular? #sc ...

The React state remains stagnant and does not receive any updates

Although there have been numerous questions on this topic before, each one seems to be unique and I haven't found a close match to my specific issue. In my scenario, I have a grid containing draggable ItemComponents. When an item is selected, additio ...

Can Angular 4 experience race conditions?

Here is a snippet of my Angular 4 Service code: @Injectable() export class MyService { private myArray: string[] = []; constructor() { } private calculate(result): void { myArray.length = 0; // Perform calculations and add results to myAr ...

Retrieve Laravel User from a Fetch Request

Attempting to extract the active user from a fetch request to my backend. Here is my front-end code: let apiToken: string | null = document.querySelector('meta[name="csrf-token"]').getAttribute('content'); fetch('http:// ...

An issue has been identified in the node_modules/xterm/typings/xterm.d.ts file at line 10, causing an error with code TS1084. The 'reference' directive syntax used

In my project, I have integrated xterm into Angular5. However, I am encountering an error when trying to run the application. Upon executing ng serve, I am facing the following error: ERROR in node_modules/xterm/typings/xterm.d.ts(10,1): error TS1084: In ...

Encountering the error "Unable to access the 'user' property of an undefined object when working with Angular and Firebase

Exploring Firebase for the first time while attempting to configure email and Google authentication in an Angular (v5) application. While following a tutorial (), I encounter an error: ERROR TypeError: Cannot read property 'user' of undefined T ...

TypeScript is encountering difficulties locating a local directory

I'm currently facing an issue with my Typescript and Node.js project in Visual Studio. The error is related to importing a local folder called "source" into my top-level file, application.ts. However, despite having the necessary directory structure s ...

Tips for implementing UI properties in React

Utilizing an external UI Library, I have access to a Button component within that library. My goal is to create a customized NewButton component that not only inherits all props from the library Button component but also allows for additional props such as ...

Can a function be invoked using a variable as a parameter in JavaScript?

Imagine a scenario where the need arises to use a conditional statement that performs the same action but calls a different method. const c = true; // just for illustration let b = ''; if (c) { b = 'method1'; } else { b = 'met ...

incorporating my unique typographic styles into the MUI framework

I'm currently working on customizing the typography for my TypeScript Next.js project. Unfortunately, I am facing difficulties in configuring my code properly, which is causing it to not work as expected. Can someone kindly provide assistance or guida ...

What is the reason behind tsc (Typescript Compiler) disregarding RxJS imports?

I have successfully set up my Angular2 project using JSPM and SystemJS. I am attempting to import RxJS and a few operators in my boot.ts file, but for some reason, my import is not being transpiled into the final boot.js output. // boot.ts import {Observa ...

Showcasing JSON information within a dropdown menu

In my project, I am working with two different JSON files named contacts and workers. Currently, I am using *ngFor to display the name of the contacts. In addition, I am also displaying the assigned workers for each contact in a dropdown, as shown below: ...

Using TypeScript with React's forwardRef

Here's my code where I have utilized React's forwardRef: interface PropsDummy {} const ProfileMenu = forwardRef<HTMLInputElement, PropsDummy>((props, ref) => { console.log(ref.current); } However, I'm encountering a TypeScript e ...

"What is the best way to apply multiple filters to an array in a React

Is there a way to incorporate dropdown menus along with search text input for filtering an array in React? I would like to give users the option to select a location and then search for specific results within that location. Any suggestions on how to ach ...

A series of OR interfaces in TypeScript Interface

Imagine a scenario where there is a choice between multiple interfaces interface a {x:string} interface b {y:string} interface c {z:string} type all = a | b | c Now, consider an object fulfilling all by being of type c When you try to access the propert ...

Error: The value of "$tweetId" cannot be parsed as it is set to "undefined". Please ensure that string values are properly enclosed

I am utilizing sanity, and if you require more details, I will furnish it promptly. When I try to access http://localhost:3000/api/getComments, I encounter the following error message: ClientError: Unable to process value of "$tweetId=undefined". Kindly ...

Tips for managing errors when utilizing pipe and mergemap in Angular

In the code snippet provided, two APIs are being called. If there is an error in the first API call, I want to prevent the second API call from being made. Can you suggest a way to handle errors in this scenario? this.userService.signUp(this.signUpForm.v ...

Error encountered when duplicating routes in Nest.js and Fastify

Using Nest.js with the Fastify adapter, I have implemented a BatchController class with a create method. The class is decorated with @Controller('batch') and the method with @Post('create'). This setup should result in a single route; h ...

User authentication status is only unavailable within the component when redirection occurs

I have successfully implemented my dasboard component, which displays the user's auth.uid and projects fetched from firestore. However, there is a line //if (!auth.uid) return <Redirect to='/signin'/> that causes issues when uncommente ...