Check out the code snippet below:
function getUsername(): string {
return 'john_doe';
}
function sampleFunction(): void {
const data = {};
const username: string = getUsername();
const age: any = 30;
data[username] = age; // ERROR: TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
}
The error is visible because noImplicitAny
is turned on for better code quality. What is a cleaner way to set this property in this context?