There is a function that returns an object:

const Data = (arg0: string, arg1: number) => { return {arg0: arg0, arg1: arg1} }; 

This object is passed to another function:

 setData(Data("0", 1)); 

Is it correct to set the type of the argument in the setData constructor like this:

 const setData = (data: Data) => {...} 

Or does it only mean that the function itself is passed, and not its result?

  • Could it be a typeScript? - vp_arth
  • This is React Native - i am so lame

2 answers 2

No, thus specifying the type incorrectly, the transmitted object is not an object of type Data.

You need something like this:

 class Data { arg0: string; arg1: number; constructor(arg0: string, arg1: number) { this.arg0 = arg0; this.arg1 = arg1; } } const setData = (data: Data) => {...} setData(new Data('answer', 42)); 
  • Wonderful, thank you! And you can still have a question about enums: const Enum = Object.freeze({type0: 'type0', type1: 'type1'}); pass as enum: Enum , or maybe enum: Enum[any] ? - i am so lame
  • Enum is just the name of a constant, this is not true. Create a new question - vp_arth

Javascript is not a typed language.

So no, the argument type is incorrect.

Your syntax calls my interpreter

 Uncaught SyntaxError: Unexpected token : 
  • this does not happen for me, the code works and moreover in WebStorm in autocomplicate to the right of the function name is displayed [Data] data - i am so lame