interfaceDEPT{name: string;age: number,[key: string]: any
}
解决方式
// 用于测试constdept:DEPT={name:"Smile",age:18}
keyof
functiondepartment(obj:DEPT){letkey:(keyof DEPT);for(key in obj){console.log('info:', obj[key])}}department(dept)
给对象声明一个接口
interfaceDEPT{name: string;age: number,[key: string]: any
}functiondepartment(obj:DEPT){for(let key in obj){console.log('info:', obj[key])}}department(dept)
泛型
function department<Textendsobject>(obj:T){for(let key in obj){console.log('info:', obj[key])}}department(dept)
as any
functiondepartment(obj:DEPT){for(let key in obj){console.log('info',(obj as any)[key])}}department(dept)
as any
functiondepartment(obj:DEPT){for(let key in obj){console.log('info',(obj as any)[key])}}department(dept)