TS编译器无法识别导入的类

时间:2019-12-03 13:51:34

标签: typescript webpack

运行Web应用程序并单击我的“开始”按钮时,出现console.error错误,告诉我Uncaught ReferenceError: Player is not defined at HTMLButtonElement.<anonymous>。如果我尝试在事件侦听器范围之外实例化Uncaught ReferenceError:Player is not defined类,则此错误将转换为Player

Player是唯一出现此问题的课程。



//app.ts
/// <reference path="game.ts" />
/// <reference path="player.ts" />
document.getElementById('startGame')!.addEventListener('click', ()=>{
        const player:Player= new Player();
        player.name=Utility.getInputValue('playername');




//player.ts
class Player implements Person{
    name!:string;
    age?:number;
    highScore!:number;
}



//utility.ts
class Utility{
    static getInputValue(elementId:string):string{
        let inputElement:HTMLInputElement=(<HTMLInputElement>document.getElementById(elementId));
        return inputElement.value;
    }

webpack配置:

const path=require('path');
module.exports = {
  entry: './js/app.js',
  devServer: {
    inline: false
  }
};

1 个答案:

答案 0 :(得分:0)

使用这些选项编辑了我的tsconfig,它起作用了:

{
    "compilerOptions":{
        "outFile": "../js/app.js"
    },
"files": [
    "./app.ts"
]
}

显然,使用三斜杠指令/// <reference path="..." />需要配置outFilefiles