Reputation: 6872
I have such case:
interface MoverShaker {
getStatus(): { speed: number; frequency: number; };
}
function GetMoverShaker() : MoverShaker {
return {
getStatus: () => { speed: 2, frequency: 3 }
}
}
I am getting such error: The name 'frequency' does not exist in the current scope. Is such construction possible in TypeScript? If I am using such construction then everything is ok:
function GetMoverShaker(): MoverShaker {
return {
getStatus: () => {
return { speed: 2, frequency: 3 }
}
}
Upvotes: 76
Views: 24133
Reputation: 37
In your first statement you do not return an object.
If you want to disregard the return
keyword in an arrow function that returns an object
you will need to wrap the directly returned value in ()
.
Which will make your function look like this instead
function GetMoverShaker() : MoverShaker {
return {
getStatus: () => ({ speed: 2, frequency: 3 })
}
}
Upvotes: 3
Reputation: 23214
You can add parens:
() => ({x:1,y:2})
This makes the parser understand that the { is not the beginning of a code block.
Upvotes: 186