TypeScript 中 for...in 遍历对象时的类型陷阱与安全写法
在 TypeScript 项目里用 for...in 遍历一个普通对象,看起来人畜无害,但如果你打开了 strict 模式,很快就会撞上一个类型报错:Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'User'。这个问题在数据格式化、配置合并、表单字段校验等场景里很常见,尤其是从后端拿到一个结构固定的对象后,想动态遍历它的键做处理。报错的根源在于 for....