2

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

7
  • 1
    Is there any problem with useState([]); or return [] correspondingly? Commented Nov 8, 2019 at 19:39
  • I am probably missing something but with react's useState it 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. Since useState is likely taking a generic type I think I need to declare that somewhere to make all the types match. Commented Nov 8, 2019 at 19:43
  • I'm not sure how code from your comment correlates with your original question. Hence, is there a reason to declare a type explicitly? Cannot TS infer it for you? Commented Nov 8, 2019 at 19:45
  • See above, just clarified shortly after you replied but it can't really infer it because I believe useState is taking a generic. Commented Nov 8, 2019 at 19:47
  • I did see it, my question still stands: any problem with NOT declaring the type explicitly and using type inference? Commented Nov 8, 2019 at 19:47

1 Answer 1

2

No need to declare them explicitly - typescript would infer it for you, hence

interface SomeType {
 name: string
}
useState([]);
function getArrayOfSomeType():SomeType[] {
  return [];
}
interface MapOfSomeTypes {
 [key:string]: SomeType
}

function getMapOfTypes():MapOfSomeTypes {
  return {};
}
Sign up to request clarification or add additional context in comments.

8 Comments

This example is not not inferring it, you're creating a function that basically casts an inferred any[] to the return type of the function?
@JuanMendes how can you tell that?
If you don't explicitly say SomeType[] as the return value of getArrayOfSomeType(), it will be inferred to be any[]. However, your function casts it to SomeType[] when it returns it?
"If you don't explicitly say SomeType[] as the return value, it will be any[]," --- how can you tell that? What makes you think it's not inferred based on the argument or return value type? Or at least that it's not inferred as [] type?
It's inferred by the caller of getArrayOfSomeType, but within the function, it's just as if you had instead done return [] as SomeType[], so you're just hiding the casting behind the function, but it's still casting
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.