In TypeScript, I declare an interface like this:
export default interface MyDTO {
readonly num: string;
readonly entitle: string;
readonly trb: string;
readonly ucr: string;
readonly dcr: string;
readonly udm?: string;
readonly ddm?: string;
}
With a function, I would like to access the value of a property, whose name is contained in a variable.
private doSomething(dto: MyDTO, property: string): any {
let label: any;
if (['dcr', 'ddm'].includes(property)) {
label = doSomethingElse(dto[property]);
} else {
label = dto[property];
}
return label;
}
Unfortunately, TypeScript gives me the following error message :
Element implicitly has an 'any' type because expression of type
'string' can't be used to index type 'MyDTO'. No index signature
with a parameter of type 'string' was found on type
'MyDTO'.ts(7053)
Anyone have an idea, please ?
Thank you
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…