Answer the question
In order to leave comments, you need to log in
How to properly describe an object in a TypeScript model?
Hello! I just started learning TypeScript and when describing the types used in the model, an error occurs, namely "Object literal may only specify known properties, and 'value' does not exist in type '{ [key: number]: any; }'"...
how to correctly set the type of the _total object and its internal values?
export default class Model {
_total: { [key: number]: any } = {};
constructor() {
super();
this._total= {
value: 1,
btn: {
add: true,
rem: false
}
};
}
}
Answer the question
In order to leave comments, you need to log in
Well, your _total key is not a number, it's a string
UPD: here it is described about the index signature https://basarat.gitbooks.io/typescript/docs/types/...
export default class Model {
_total: { [key: string]: any } = {};
constructor() {
super();
this._total['value'] = 1;
this._total['btn'] = {
add: true,
rem: false
};
}
}
class Total {
value: number,
btn: {
add: boolean,
rem: boolean
}
};
export default class Model {
_total: Total;
constructor() {
super();
this._total= new Total {
value: 1,
btn: {
add: true,
rem: false
}
};
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question