I have a Object like this:
let myObject = {key:[]};
And I try to push some data to the key in the myObject:
myObject.key.push({name:'Mostafa',family:'Saadatnia'});
What is the typescripts error tells me is:
Type 'any' is not assignable to type 'never'.ts(2322)
How can I fix that? I don't want to fill data on definition-time. All of I need is fill that in the runtime
CodePudding user response:
So, when you assign something as []
, TS assumes that its type is []
, ie, an array of length 0. To avoid this, you can obviously go the any
route, but then you don't get type-checking. The proper way to do this is to give the key property a type:
type Obj = {
name : string;
family: string;
}
let myObject:{key: Obj[]} = {key:[]};
myObject.key.push({name:'Mostafa',family:'Saadatnia'});
CodePudding user response:
All of you need is add a kind of data type to your object myObject
for example any
:
let myObject:any = {key:[]};
myObject.key.push({name:'Mostafa',family:'Saadatnia'});
Your codes works as well.
CodePudding user response:
that error is not related with these files