I've the following interface.
interface IDetails {
name: string;
age: number;
}
interface IConfig {
appName: string;
userDetails: IDetails;
[key: string]: string | IDetails
}
const details: IConfig = {
appName: 'test',
userDetails: {
name: 'xyz',
age: 67,
},
}
const t: string = 'userDetails'
const name: string = details[t].name
// ^^^^
// throws the following error
//
// Property 'name' does not exist on type 'string | IDetails'.
// Property 'name' does not exist on type 'string'.
When I try to assign multiple types/interfaces to the key signature, I get this error. I know I can use [key: string]: any. But, I don't want to generalize it. Is there any possible way to make this work?
(<IDetails>details[t]).namestring | IDetailsis not working?[key: string]: string | IDetails? without it what you're trying would just work. Are you expecting more properties?tis the example will be dynamic. So, if I need to access the properties, I need to do this.