1

I am learning TypeScript, and I have come across a strange bit of code that I can't seem to find specific documentation on. It looks like:

function retTest(): {valid:string, int:number} {
    let int = 42;
    return {valid:"foo",int};
}

The object being returned does not specify the "int" attribute as a name:value pair like the "valid" attribute. However, this only works if the name of the scoped variable matching the attribute name in the return type definition. E.g. this is not valid:

function retTest(): {valid:string, int:number} {
    let badInt = 42;
    return {valid:"foo",badInt};
}

It's like the attribute name is automatically inferred from the variable name or something.

What is the name of this behavior/feature of TypeScript? What is a good reference explaining it in detail?

OliverRadini
  • 6,238
  • 1
  • 21
  • 46
aaa90210
  • 11,295
  • 13
  • 51
  • 88
  • 2
    [Here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer) is another another explanation. – ford04 Jan 10 '20 at 10:19
  • 1
    Also, for reference, this isn't a Typescript specific feature; it's the [javascript shorthand property](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer) – OliverRadini Jan 10 '20 at 10:36

0 Answers0