Home > Blockchain >  Type for object in React/Typescript
Type for object in React/Typescript

Time:11-09

What is the type for a object in React/Typescript for example:

const [value, setValue] = React.useState<any>({});

if I need to define it as an object what type would I put in, instead of <any>

CodePudding user response:

interface MyObject {
  [k: string]: any;
}

const [value, setValue] = React.useState<MyObject>({});

[k: string]: any; means that property must be a string, and it's value are any.

CodePudding user response:

You would use your own type, for example. Refer to other SO threads like this one: Set types on useState React Hook with TypeScript

If you have a generic object and you only need to define it is some object with properties and values, you can simply define it like this const [value, setValue] = React.useState<{[key: string] : any}>({});

  • Related