首页 前端知识 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

2024-04-29 11:04:32 前端知识 前端哥 234 735 我要收藏

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://www.qianduange.cn//article/6194.html
标签
评论
还可以输入200
共0条数据,当前/页
发布的文章

JQuery中的load()、$

2024-05-10 08:05:15

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!