0

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>

2
  • 1
    If you know that it is an object but you don't know the keys and values go with Record<PropertyKey, unknown>. If you know that keys are strings, use Record<string, unknown> Commented Nov 8, 2021 at 10:16
  • Well what are you planning to put it that object? The more information you give the compiler, the more helpful it can be. Commented Nov 8, 2021 at 10:17

1 Answer 1

3
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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.