What could be causing the error in my TypeScript function that just passed successfully?

Within the app.tsx file, I have a function that looks like this:

Since the function does not return anything, I have declared the return value as void.

const addToDo = (newToDoText: string): void => {
    setToDos([...todos, { id: 4, text: newToDoText, completed: false }]);
  };

I then passed this function into a form component like so:

<ToDoForm addToDo={addToDo} />

The issue arises in the ToDoForm component when I write it in this manner:

   //Error Here
    const ToDoForm = ({ addToDo : () => void}) => {

There are two errors - one on the closing bracket }, which says an expression is expected, and another on the => arrow, which states that a semicolon is expected.

What mistake am I making here?

Answer №1

You appear to be attempting to merge property destructuring syntax with an object type annotation (but they should use separate syntax). Consider one of these alternative approaches:

TS Playground

// Define the props separately like so:
type ToDoFormProps = {
  addToDo: (newToDoText: string) => void;
};

// Utilize them as follows:
const ToDoForm1 = (props: ToDoFormProps) => {
  props.addToDo; // (newToDoText: string) => void
};

// Destructure the "addToDo" property in this manner:
const ToDoForm2 = ({ addToDo }: ToDoFormProps) => {/* ... */};

// Alternatively, define the props inline:
const ToDoForm3 = (props: { addToDo: (newToDoText: string) => void }) => {
  props.addToDo; // (newToDoText: string) => void
};

// Or merge the inline type and destructured property:
const ToDoForm4 = ({ addToDo }: { addToDo: (newToDoText: string) => void }) => {/* ... */};

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

React.js with Typescript is throwing an error stating that a property does not exist on the child component

Currently, I am working with React in conjunction with typescript 2.3.4. I keep encountering the error TS2339: Property 'name' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>'. This issue arises wh ...

Type verification not functioning properly in a standalone TypeScript function

I am trying to validate the type of a parameter in a separate function, but I keep getting this error: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable ...

Determine the class of an object within the "keyof" parameter by utilizing both property and generic types

I have a requirement to create an interface with generic types that can accept an object with keys representing "root field names" and values as arrays of objects defining sub-fields with the key as the name of the sub-field and the type as the value' ...

Ways to enhance TypeScript definitions for Node.js modules

Using the nodejs module "ws", I installed typings using typings i dt~ws import * as WebSocket from "ws"; function add(client:WebScoket){ let cid = client.clientId; } I am trying to Expand the WebSocket object with a property called clientId, but I&a ...

Conditional types allow the function parameter type to be determined based on the type of another parameter

Here: function customFunction<T extends boolean> (param1: T, param2: T extends true ? number[] : number) { if (param1) { let result: number[] = param2; // Type 'number' is not assignable to type 'number[]'.(2322) } } ...

Unable to extract the 'data' property from an undefined source in React, causing an error

I encountered this error message: TypeError: Cannot destructure property 'data' of '(intermediate value)' as it is undefined. export const getServerSideProps: GetServerSideProps = async () => { // categories const { data: categor ...

Tips for generating a hyperlink in a Typescript file using Angular version 16 and above

I am encountering an issue with my consts.ts file in the project. Specifically, I have defined a constant LINK1 as <a href='https://sample.com/'>LINK 1</a>; However, this setup is not working as expected. What I actually want is to d ...

Ways to incorporate suspense with NextJS 14 - how can I do it?

I am looking to add a suspense effect to the initial loading of my page while the assets are being fetched. These assets include images on the home screen or as children of the RootLayout component. How can I implement an initial Loading state for these ...

Demonstrating reactivity: updating an array property based on a window event

One example scenario involves setting specific elements to have an active class by assigning the property "active" as true (using v-bind:class). This property is modified within a foreach loop, after certain conditions are met, through the method "handleSc ...

Substitute all attributes of objects with a different designation

I need to update all object properties from label to text. Given: [ { "value": "45a8", "label": "45A8", "children": [ { "value": "45a8.ba08", "label": "BA08", &q ...

Tips for choosing and unchoosing rows in angular 6

I am looking to extract the values from selected rows and store them in an array. However, I also need to remove a row from the result array when it is deselected. The issue with my current code is that every time I click on a row, the fileName values are ...

Is there a way to monitor user engagement within my app without depending on external analytics platforms?

I'm looking to enhance the user-friendliness of my applications deployed on the Play Store by tracking users' interactions. Specifically, I want to keep track of: Screen Time: Monitoring how much time users spend on each screen. Clicks: Tracking ...

What is the best way to retrieve a cookie sent from the server on a subdomain's domain within the client request headers using getServerSideProps

Currently, I have an express application using express-session running on my server hosted at api.example.com, along with a NextJS application hosted at example.com. While everything functions smoothly locally with the server setting a cookie that can be r ...

Altering the dimensions of radio buttons

I am a newcomer to using material-ui. I am currently working on incorporating radio buttons in a component and would like to reduce its size. While inspecting it in Chrome, I was able to adjust the width of the svg icon (1em). However, I am unsure how to a ...

Encountering issues when attempting to set up graphqlExpress due to type

This is my first experience using GraphQL with Express. I have created a schema: import { makeExecutableSchema } from "graphql-tools"; import { interfaces } from "inversify"; const schema = ` type Feature { id: Int! name: String ...

Creating an interface and setting a default value

I am exploring the use of interfaces in my models and want to establish a default value for them. export interface IPerson { id: string; name: string; } class Person implements IPerson { id = ''; name = 'John'; } export cla ...

Tips for resolving the error message "Cannot assign type 'string' to type '...' in NextJS"

In my nextjs-app, there is a Button component implemented as follows: interface IButton { text: string theme: 'primary' | 'secondary' size: 'small' | 'medium' | 'large' onClick?: () => void } ...

Issues with typescript compiler when using React-beautiful-dnd

I recently updated react and react-beautiful-dnd to the newest versions and now I am encountering many type errors in my code: {sortedDimensions.map((dimension: any, index: number) => ( <Draggable key={index} ...

Error: Import statement cannot be used outside a module (@cucumber/cucumber) while using Node.JS, Playwright, and Cucumber framework

I encountered an issue while attempting to compile my Node.js code that is compliant with ECMAScript 6: $ npx cucumber-js --require features/step_definitions/steps.ts --exit import { Before, Given, When, Then } from "@cucumber/cucumber"; ^^^^^^ ...

The asyncData and fetch functions are failing to populate the data

I am currently working with nuxt v2.14.1 along with typescript and the nuxt-property-decorator package. I've been encountering a variety of issues. One of the problems I'm facing is the inability to set data from fetch() or asyncData. console. ...