【发布时间】:2019-10-23 04:58:22
【问题描述】:
我正在 Electron 中创建一个 hello world 项目,发现我可以将 Typescript 用于主进程 https://electronjs.org/blog/typescript。
它说使用 Typescript 将文件扩展名从 index.js 更改为 index.ts,然后更新 package.json 以指向新脚本:
{
"name": "electrontypescript",
"version": "1.0.0",
"description": "Typescript and Electron",
"main": "index.ts",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "^5.0.1"
},
"dependencies": {
"lodash": "^4.17.11"
}
}
它可以工作,但是当我去添加我自己的类时,它会抛出错误。
index.ts 顶部:
const { TypeHouse } = require ("./TypeHouse");
TypeHouse.ts:
function test() {
}
export class Cat {
}
export class TypeHouse {
public status: String = "ready";
private _output: String = "";
readonly startTime = Date.now();
private running: Boolean = false;
constructor(private _message: String, private _prompt: String) {
this.setStatus(_message);
}
async execute(): Promise<void> {
try {
//await CommandExecutor.execute(this);
} catch (exception) {
this.handleError(exception);
} finally {
//this.emit("end");
}
}
handleError(message: TypeHouse | string): void {
this.setStatus("Status.Failed");
if (message) {
//
}
}
isRunning(): boolean {
return this.running !== false;
}
public setStatus(value: String) {
this._output = value;
}
}
module.exports = {TypeHouse, Cat};
包.json:
{
"name": "electron-app",
"version": "1.0.0",
"description": "Electron",
"main": "index.js",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "^5.0.1",
"typescript": "^3.5.1"
},
"dependencies": {
"lodash": "^4.17.11"
}
}
错误信息:
应用程序在加载期间抛出错误错误:找不到模块'./TypeHouse' 需要堆栈: - /Users/projects/ElectronApp/index.ts - /Users/projects/ElectronApp/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js
如果重要的话,我正在使用 Visual Studio Code(它会在控制台中引发错误)。
【问题讨论】:
标签: typescript visual-studio-code electron