如何在打字稿中导入嵌套接口
IT打字稿导入嵌套接口时,VS代码中的preadiveatext在高级接口中显示我的项目,但在嵌套的界面中不显示。
“示例”接口中有一个[key:string]意味着我不能将PredivateTatext用于此类数据结构(即使它将检查类型安全性),或者它只是无法正常设置它。
示例
export interface Item {
id: number;
size: number;
}
export interface Example {
name: string;
items: {
[key: string]: Item
};
}
然后file 2
import {Example, Item} from '../utils/interfaces'
const obj = {
name: "test",
items: {
"a": {
id: 1,
size: 10
},
"b": {
id: 2,
size: 34
}
}
}
结果,
proadiCterText适用于第一个级接口,但在此之后不做
console.log(obj. // predictatext offers two options: items & names
console.log(obj.items. // predictatext offers : NOTHING
It Typescript when I import nested interfaces the predictatext in VS code shows me items in the high level interface but not in the nested one.
There is a [key:string] in the "Example" interface does this mean I cannot use the predictatext for data structures of this sort (even though it will check type safety), or is it just not working as I have set it up incorrectly.
Example
export interface Item {
id: number;
size: number;
}
export interface Example {
name: string;
items: {
[key: string]: Item
};
}
Then file 2
import {Example, Item} from '../utils/interfaces'
const obj = {
name: "test",
items: {
"a": {
id: 1,
size: 10
},
"b": {
id: 2,
size: 34
}
}
}
RESULT
The predictertext works for the first level interface but not after that eg
console.log(obj. // predictatext offers two options: items & names
console.log(obj.items. // predictatext offers : NOTHING
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题不是嵌套接口。以下内容也不会自动完成:
不能自动完成,因为键可能是任何东西。同样,
obj.items。
之后的任何内容都是有效的。如果仅允许某些项目,则必须在接口中指定它们。
The problem isn't the nested interfaces. The following won't autocomplete either:
It can't autocomplete because the key could be anything. Similarly, anything after
obj.items.
is valid.If only certain items are allowed, you would have to specify them in the interface.