首页 前端知识 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 前端知识 前端哥 189 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
标签
评论
发布的文章

@JsonCreator和@JsonValue

2024-05-05 22:05:05

Python 字符串转换为 JSON

2024-05-05 22:05:00

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