I have an object type:
interface ShortUrlParam {
openid: string;
avatar: string;
nickname: string;
}
const param: ShortUrlParam = {
openid: 'abc123',
avatar: '',
nickname: 'wenzi'
}
let query = '';
for(let key in param) {
query += `&${key}=${encodeURIComponent(param[key])}`; // error tip
}
in param[key], error tip is:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'ShortUrlParam'. No index signature with a parameter of type 'string' was found on type 'ShortUrlParam'.ts(7053)
I have two programmes, but not perfect.
1. redefine interface ShortUrlParam
interface ShortUrlParam {
openid: string;
avatar: string;
nickname: string;
[key: string]: string;
}
2. param as any
for(let key in param) {
query += `&${key}=${encodeURIComponent((param as any)[key])}`;
}
my question is there has any better solution?