我希望将我的自定义对象分配给在Prisma包中实现的JsonObject。这段代码会产生错误,我不知道为什么:
interface JsonArray extends Array<JsonValue> {}
type JsonValue = string | number | boolean | JsonObject | JsonArray | null
type JsonObject = {[Key in string]?: JsonValue}
// ^ These are defined in Prisma package and I cannot change them
interface Object {
name: string
}
let object : Object
let jsonObject : JsonObject
// This line has error
jsonObject = object;
// Type 'Object' is not assignable to type 'JsonObject'.
// The 'Object' type is assignable to very few other types.
// Did you mean to use the 'any' type instead?
// Index signature for type 'string' is missing in type 'Object'.ts(2322)发布于 2022-02-05 12:50:07
首先,不要将自定义对象命名为Object。Object类型是为Object值构建的类型。这可能会让人迷惑。我不确定您是想更新现有的Object类型,还是只想创建一些自定义类型,这对于接口来说非常重要,因为声明合并的存在。
考虑一下这个例子:
interface JsonArray extends Array<JsonValue> { }
type JsonValue = string | number | boolean | JsonObject | JsonArray | null
type JsonObject = { [Key in string]?: JsonValue }
interface CustomObject {
name: string
}
declare let object: CustomObject
declare let jsonObject: JsonObject
// This line has error
jsonObject = object;您所拥有的错误意味着jsonObject已被索引。换句话说,它意味着您可以使用任何字符串来访问对象值。例如:
jsonObject['hello'] // ok而对于object则不是这样。在object情况下,只允许使用name键访问适当的值。您不允许使用hello。
object['hello'] // error现在,想象一下TS允许您执行以下操作的情况:
jsonObject = object;
jsonObject['hello'] // undefined, because it has only `name` property因此,如果要使其可分配,则应将索引添加到CustomObject中。
interface CustomObject {
[prop: string]: JsonValue
name: number
}或者,更有趣的是使用type关键字来声明CustomObject而不是interface
interface JsonArray extends Array<JsonValue> { }
type JsonValue = string | number | boolean | JsonObject | JsonArray | null
type JsonObject = { [Key in string]?: JsonValue }
type CustomObject= {
name: number
}
declare let object: CustomObject
declare let jsonObject: JsonObject
jsonObject = object; // no error
jsonObject.name是的,interface和type在索引方面存在差异。见我的回答和文章
请记住,这一行动是允许的:
jsonObject['hello'] // undefined but allowed没有编译器错误,但它不安全。
发布于 2022-02-05 12:53:59
这里有几个问题。
Object是内置的javascript数据类型。你应该给你的接口命名其他的东西,比如MyObject,object,但它没有初始化,也就是说它没有任何值。你不能像x = object那样在操作中使用它,JsonObject完全重叠。您可以修改接口或使用type。interface JsonArray extends Array<JsonValue> {}
type JsonValue = string | number | boolean | JsonObject | JsonArray | null
type JsonObject = {[Key in string]?: JsonValue}
// ^ These are defined in Prisma package and I cannot change them
type MyObject = {
name: string
}
let anObject: MyObject = {name: "foo"}
let jsonObject: JsonObject
// no error
jsonObject = anObject在打字稿操场里试一试。
https://stackoverflow.com/questions/70997988
复制相似问题