Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Typescript what does a type annotation with two types between two curly braces mean?

Tags:

typescript

An example:

function doSomething(): { string, SomeClass } {
 ...
} 

From playing around with the compiler it seems that it will accept any return value that is an object with any number of string keys and SomeClass values.

So something like this:

{
 "key1": someClass1,
 "key2": someClass2,
  ...
}

Is my understanding correct?

--Update--

It seems like the annotation I need is { [key: string]: SomeClass }

like image 743
Nelson Avatar asked Dec 18 '22 02:12

Nelson


1 Answers

Nope - that function signature means 'this function will return an object that has the keys string and SomeClass, and any values'. This means that this will compile fine:

function doSomething(): { string, SomeClass } {
  return {
    string: false,
    SomeClass: 1
  }
} 

It's functionally equivalent to:

function doSomething(): { string: any, SomeClass: any }
like image 146
Joe Clay Avatar answered May 28 '23 12:05

Joe Clay