How to declare a state variable of type Set()?

Viewed 665

I'm trying to create a state variable in React with Typescript. It throws an error when I try to define its type to Set.

This variable is suppose to hold an array of objects.

const [blocksList, setBlocksList] = useState<Set>([]);

I'm sure I am doing something wrong here but I haven't found any help on internet so far. Please let me know if anyone needs more information on my question. Thanks

1 Answers

Set is a generic type. Here (ctrl + click on a type) we can see that it takes 1 argument. enter image description here

You should to pass a type to a Set. For example:

type MyType = { some: string };
const [blocksList, setBlocksList] = useState<Set<MyType>>(new Set());

Note: you should to pass in the useState new Set() instead []

Related