TS报错 Don’t use object
as a type. The object
type is currently hard to use
问题来源
function func<T extends object, S extends object>() {}
当使用泛型继承object
属性时, typescript
就会提示问题
Don't use `object` as a type. The `object` type is currently hard to use ([see this issue](https://github.com/microsoft/TypeScript/issues/21732)).
Consider using `Record<string, unknown>` instead, as it allows you to more easily inspect and use the keys @typescript-eslint/ban-types
问题来源可以查看提示的issue
的链接
https://github.com/microsoft/TypeScript/issues/21732
其实解决办法也可以查看typescript
的提示, 通过Record<string, unknown>
替换object
function func<T extends Record<string, unknown>, S extends Record<string, unknown>>() {}
拓展-Record 链接
Record
是typescript
内置的高级类型
- 主要用途为了定义对象的
key
和value
类型, 用来显示对象键的类型和值的类型
使用方法, 定义对象a
的键是string
类型, 值是string | number
的联合类型
const a: Record<string, string | number> = {
'name': '小火车',
'age': 18
}