I'm creating a hello world project in Electron and found out I can use Typescript for the Main process, https://electronjs.org/blog/typescript.
It says to use Typescript change the file extension from index.js to index.ts and then update the package.json to point to the new script:
{
"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"
}
}
It works but when I went to add my own class it throws errors.
Top of 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};
Package.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"
}
}
Error message:
App threw an error during load Error: Cannot find module './TypeHouse' Require stack: - /Users/projects/ElectronApp/index.ts - /Users/projects/ElectronApp/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js
I'm using Visual Studio Code if it matters (it throws the error in the console).