当前位置:网站首页>TS报错 Don‘t use `object` as a type. The `object` type is currently hard to use

TS报错 Don‘t use `object` as a type. The `object` type is currently hard to use

2022-07-01 14:55:00 小火车况且况且

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 链接

Recordtypescript内置的高级类型

  • 主要用途为了定义对象的keyvalue类型, 用来显示对象键的类型和值的类型
    使用方法, 定义对象a的键是string类型, 值是string | number的联合类型
const a: Record<string, string | number> = {
    
	'name': '小火车',
	'age': 18
}
原网站

版权声明
本文为[小火车况且况且]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43972992/article/details/125546692