Oleg
Oleg

Reputation: 686

Resulting type of object literal

From specification:

When an object literal is contextually typed by a type that includes a string index signature, the resulting type of the object literal includes a string index signature with the union type of the types of the properties declared in the object literal, or the Undefined type if the object literal is empty

What does it mean - the resulting type of the object literal?

Let say we have

var a:{[key:string]:any} = {a:1,b:"2"};

a has type {[key:string]:any}

Where is the resulting type of object literal in this declaration?

Is there any places in code where I can see resulting type of object literal in action?

Upvotes: 0

Views: 265

Answers (1)

basarat
basarat

Reputation: 276313

Is there any places in code where I can see resulting type of object literal in action?

Following code sample

declare const str: 'a' | 'b';
let foo = { a: 1, b: "2" }[str]; // typeof foo is string | number

Upvotes: 1

Related Questions