I am using the latest version of Typescript: 2.6.2 .
I encounter a strange situation where, if I do foo({a:1,b:2}) , everything does not work, and if I do: foo({b:2,a:1}) , they work.
I have a generic class, an interface that has 2 properties and a function.
Here is the code:
class MyClass<T> { value: T; next(value: T): void { } } export enum StateKey { backlogItems='backlogItems'} export interface State { backlogItems : number[]; [key: string]: any } class A { private subj = new MyClass<State>(); public set<T>(name: StateKey, state: T) { this.subj.next({ backlogItems: [...this.subj.value.backlogItems] , [name]:state
I get an error message:
Argument of type '{ [name]: T; }' is not assignable to parameter of type 'State'. Types of property 'backlogItems' are incompatible. Type 'T' is not assignable to type 'number[]'.
But if I change the order of the literals in the object:
from:
this.subj.next({ backlogItems: [...this.subj.value.backlogItems], [name]: state })
to:
this.subj.next({ [name]:state, backlogItems: [...this.subj.value.backlogItems] })
Question
Why does reordering make it compile?
Image ID
Typescript Playground Demo
javascript typescript
Royi namir
source share