简体   繁体   English

语法错误 - 打字稿 - 意外标记:?

[英]Syntax error - typescript - unexpected token :?

I am new to Angular, and as you know to be a good Angular developer you have to be good in typescript, so I wrote my simple program as you can see :我是 Angular 的新手,你知道要成为一名优秀的 Angular 开发人员,你必须擅长打字,所以我编写了我的简单程序,如你所见:

function loge(messag){
    console.log(messag);
}

var message:string;

message = "Hi";

loge(message); 

There is no Syntax error I believe But I get this in my Terminal :我相信没有语法错误但我在终端中得到了这个:

SyntaxError: Unexpected token :
  at createScript (vm.js:80:10)
  at Object.runInThisContext (vm.js:139:10)
  at Module._compile (module.js:607:28)
  at Object.Module._extensions..js (module.js:654:10)
  at Module.load (module.js:556:32)
  at tryModuleLoad (module.js:499:12)
  at Function.Module._load (module.js:491:3)
  at Function.Module.runMain (module.js:684:10)
  at startup (bootstrap_node.js:187:16)
  at bootstrap_node.js:608:3

I don't know why I get this, I need to help so I can move on.我不知道为什么我会得到这个,我需要帮助才能继续前进。 I am using the version 2.5.3 of typescript and PhpStrom as an IDE.我正在使用 2.5.3 版的 typescript 和 PhpStrom 作为 IDE。

Any help would be much appreciated.任何帮助将非常感激。

Type annotations don't exist in JavaScript, so this line: JavaScript 中不存在类型注释,所以这一行:

var message:string;

Should be:应该是:

var message

Remember to transpile your TypeScript to JavaScript - and reference the JavaScript file in your application.请记住将您的 TypeScript 转换为 JavaScript - 并在您的应用程序中引用 JavaScript 文件。

First you need to transpile your typescript into javascript.首先,你需要transpile您的打字稿成JavaScript。 Then you are able to use them in your browser environment.然后您就可以在浏览器环境中使用它们。

Put a tsconfig.json like below into your project folder将如下所示的 tsconfig.json 放入您的项目文件夹中

{
  "compilerOptions": {
      "outDir": "./dist/",
      "sourceMap": true,
      "module": "ES6",
      "target": "ES6",
      "baseUrl": "./src/"
  },
  "include": [
      "./src/**/*"
  ]
}

Put your typescript file into ./src and run tsc .将您的打字稿文件放入./src并运行tsc Then the transpiled file will be in ./dist .然后转换后的文件将在./dist Use them in your browser will work在浏览器中使用它们会起作用

I also was getting the same problem because I was trying to compile the .ts file and then trying to run the same .ts file using node file_name.ts command.我也遇到了同样的问题,因为我试图编译 .ts 文件,然后尝试使用 node file_name.ts 命令运行相同的 .ts 文件。

But it should be like first compile .ts file using tsc file_name.ts then run the generated js file using node file_name.js.但它应该像首先使用 tsc file_name.ts 编译 .ts 文件,然后使用 node file_name.js 运行生成的 js 文件。

I had the same problem.我有同样的问题。 The problem is that I was trying to run the .ts file with node, which is actually a JS interpreter, so that gave me the error.问题是我试图用 node 运行 .ts 文件,这实际上是一个 JS 解释器,所以这给了我错误。 the sequence of steps is to first call the TS compiler, and then node.步骤顺序是先调用TS编译器,再调用node。 tsc file.ts then node file.js tsc file.ts 然后节点 file.js

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

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