所以现在,我正在尝试制作一个小型模块化 Web 应用程序。我正在使用 typescript ,我对它还很陌生。
在我的代码中,我:
- 定义接口(interface)的结构 (
ProgramList
) - 我创建一个对象,该对象具有
ProgramInfo
类型的一个属性 (ProgramList
) - 我使用
ProgramList
接口(interface)用一个数组项定义ProgramList
。
这是我的代码:
interface ProgramInfo {
path:string;
name:string;
pkgname:string[];
start?:string[];
cli?:string[];
}
let program = {
ProgramList: ProgramInfo[]
};
program.ProgramList = [
{
path: "/default_programs/WOSPMan",
name: "WOSPMan (WebOS Package Manager)",
pkgname: ["com", "webos", "wospman"],
start: ["wospman", "wospm"],
cli: ["wospman"]
}
];
我的 IDE (JetBrains WebStorm) 不断出现 TypeScript 编译错误:
TS2304:找不到名称
ProgramInfo`
尽管我在前几行明确定义了 ProgramInfo
。
最佳答案
当你这样做时:
let program = {
ProgramList: ProgramInfo[]
};
您使用属性ProgramList
声明对象program
。但是您尝试将接口(interface)数组分配给此属性,这是没有意义的。你可以这样做:
let program: {ProgramList: ProgramInfo[]} = {
ProgramList: []
};
或者像这样:
interface Program {
ProgramList: ProgramInfo[];
}
let program: Program = {
ProgramList: []
};
关于javascript - TypeScript 接口(interface)没有按我的预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38831527/