简体   繁体   English

在 TypeScript 中定义自定义类型时出现意外标识符

[英]Unexpected identifier when defining a custom type in TypeScript

The following code gives me an error Uncaught SyntaxError: Unexpected identifier on the line type Player = { and I don't understand what is incorrect.以下代码给了我一个错误Uncaught SyntaxError: Unexpected identifier on line type Player = {我不明白什么是不正确的。 Worth mentioning is I am brand new to TypeScript, is it possible to do <script src="static/ts/game.ts"></script> like you would any JavaScript file, or need I compile it into JavaScript first somehow then refer to the.js file?值得一提的是,我是TypeScript的新手,是否可以像执行任何 JavaScript 文件一样执行<script src="static/ts/game.ts"></script> ,或者我需要先以某种方式将其编译成 Z6836155AF75AF7EDD80C5AF7EDD80参考.js文件? Maybe that's where I am doing things wrong.也许这就是我做错事的地方。

type Player = {
    isRolling: boolean;
    position: number;
};

const HUMAN: Player = {
    isRolling: true,
    position: 1
};

const AI: Player = {
    isRolling: false,
    position: 1
};

const PLAYERS: Player[] = [HUMAN,AI];

The only code above that code is:该代码上方的唯一代码是:

// - Markers
const MARKER_HUMAN = document.createElement('div');
MARKER_HUMAN.className = 'marker-human';

const MARKER_AI = document.createElement('div');
MARKER_AI.className = 'marker-ai';

// - Dice
const DICE = document.getElementsByClassName('dice')[0];

/* Board Variables */
const BOARD_ROWS = 10;
const BOARD_COLS = BOARD_ROWS;

Browsers can only understand .js files at the moment: therefore, you cannot load typescript files directly via the <script> tags: it will need to be compile into JS first using the typescript compiler tsc .浏览器目前只能理解.js文件:因此,您不能直接通过<script>标签加载 typescript 文件:需要先使用 typescript 编译器tsc将其编译为 JS。

Your IDE probably supports TypeScript compilation, but you need to check the configuration for your particular IDE.您的 IDE 可能支持 TypeScript 编译,但您需要检查特定 IDE 的配置。 Otherwise you can always compile TypeScript via the command line .否则,您始终可以通过命令行编译 TypeScript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM