Because of how arrow function notation works, the curly braces around the output are interpreted as containing the function body instead of a value being returned.
There are two ways around this. One is to use the longer arrow function notation, with a return
statement. The other is to wrap your object in brackets.
It also looks like you've forgot to add the y
and z
labels in your output.
Try one of these:
let foo: (x: number) => {y: number, z: number} = (x) => ({y: x+1, z: x+2});
let foo: (x: number) => {y: number, z: number} = (x) => {
return {y: x+1, z: x+2};
};
It's also worth noting that when initialising a variable in this way, TypeScript is able to infer its type. So you can actually shorten your code to just one of these:
let foo = (x: number) => ({y: x+1, z: x+2});
let foo = (x: number) => {
return {y: x+1, z: x+2};
};