In Typescript I want to do something like this:
interface SomeType {
name: string
}
useState([]:SomeType[]); // This creates an empty array of SomeType inline & calls a function called useState
or even
function getArrayOfSomeType():SomeType[] {
return []:SomeType[];
}
Other cases might involve the same concept but with a map/dictionary:
// Maybe there is a better way to declare maps?
interface MapOfSomeTypes {
[key:string]: SomeType
}
function getMapOfTypes():MapOfSomeTypes {
return {} as MapOfSometypes; // or perhaps {}:MapOfSomeTypes
}
This is to get around having to declare these before and then returning them or passing them to another function when all I need is an empty version of them (like for react-hooks).
useState([]);orreturn []correspondingly?useStateit looks like:const [elementOfSomeType, functionThatTakesSomeType] = useState({});and I can't figure out how to declare the types for them without initializing an empty object outside of this line with type:SomeType. SinceuseStateis likely taking a generic type I think I need to declare that somewhere to make all the types match.useStateis taking a generic.