typescript 打字稿错误 TS1005: ';' 预期,但我没有看到我缺少分号的地方

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41875851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:12:29  来源:igfitidea点击:

Typescript error TS1005: ';' expected but I don't see where I'm missing a semicolon

typescript

提问by Kelly

I am trying to learn Typescript and I'm following the tutorial on this page: https://www.typescriptlang.org/docs/handbook/functions.html. When I create a file cardPicker.ts and paste the following code, it won't compile. I get this error 5 times: Typescript error TS1005: ';' expected. Lines 1,6,7,14 and 15. I don't see where a semicolon is missing, but maybe the error message means something else. I'm concerned about my version of ts, but I just installed that two weeks ago. When I do "tsc -version" it says 1.0.3.0

我正在尝试学习 Typescript,并且正在学习此页面上的教程:https://www.typescriptlang.org/docs/handbook/functions.html 。当我创建一个文件 cardPicker.ts 并粘贴以下代码时,它不会编译。我收到这个错误 5 次:Typescript error TS1005: ';' 预期的。第 1、6、7、14 和 15 行。我没有看到缺少分号的地方,但错误消息可能还有其他含义。我担心我的 ts 版本,但我两周前才安装了它。当我执行“tsc -version”时,它说 1.0.3.0

let deck = {
  suits: ["hearts", "spades", "clubs", "diamonds"],
  cards: Array(52),
  createCardPicker: function() {
     return function() {
         let pickedCard = Math.floor(Math.random() * 52);
         let pickedSuit = Math.floor(pickedCard / 13);

         return {suit: this.suits[pickedSuit], card: pickedCard % 13};
     }
  }
}

let cardPicker = deck.createCardPicker();
let pickedCard = cardPicker();

alert("card: " + pickedCard.card + " of " + pickedCard.suit);

I compile the project by running "tsc cardPicker.ts" in the command line.

我通过在命令行中运行“tsc cardPicker.ts”来编译项目。

采纳答案by bug-a-lot

Is that everything you have in your ts file? Or do you have some reference comments in there too?

这就是你的 ts 文件中的所有内容吗?或者你那里也有一些参考意见?

That error doesn't complain about missing semicolon, as TypeScript doesn't require you to use them at all, just like JS in that regard.

该错误不会抱怨缺少分号,因为 TypeScript 根本不需要您使用它们,就像 JS 在这方面一样。

The error you're getting instead is because something else didn't make sense to the compiler, like a declaration that didn't end properly. ( see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/4004). For example:

你得到的错误是因为其他东西对编译器没有意义,比如没有正确结束的声明。(见https://github.com/DefinitelyTyped/DefinitelyTyped/issues/4004)。例如:

let deck: Number of Date;

Will produce that exact error.

会产生那个确切的错误。

Depending on your setup you might be compiling more than you might think.

根据您的设置,您编译的内容可能比您想象的要多。

In your case since your compiler is so old, it might just be that it doesn't understand the keyword letwhich was introduced in TS 1.4.

在您的情况下,由于您的编译器太旧了,可能只是它不理解letTS 1.4 中引入的关键字。

UPDATE

更新

To clarify, the command you used to install typescript installed latest. But in order to use it, you need to run node.js command prompt instead of the normal Windows command prompt, assuming you are on Windows.

澄清一下,您用来安装打字稿的命令是最新安装的。但是为了使用它,假设您使用的是 Windows,您需要运行 node.js 命令提示符而不是普通的 Windows 命令提示符。

回答by Airon Teixeira

when you install tsc maybe you used npm install tsc -g

当您安装 tsc 时,您可能使用过 npm install tsc -g

Following this link: https://www.npmjs.com/package/tscthis package where discontinued.

点击此链接:https: //www.npmjs.com/package/tsc此软件包已停产。

Now you should use npm install typescript -gthen you will have the newer version.

现在您应该使用,npm install typescript -g然后您将拥有更新的版本。

Today tsc -vreturn Version 2.8.1

今日tsc -v回归2.8.1 版

With newer version you do not will get ts1005

使用较新的版本,您将不会获得 ts1005

回答by Simon_Weaver

I had this very bizarre same error with errors like this :

我有这个非常奇怪的相同错误,错误如下:

Took me a little longer than it should to realize the path was duplicated

我花了比它应该的时间更长的时间才意识到路径是重复的

src/app/services/was nested inside src/app/services/MYPROJECT. This was due to a bad path in a code gen and I hadn't noticed the extra files.

src/app/services/被嵌套在里面src/app/services/MYPROJECT。这是由于代码生成中的路径错误,我没有注意到额外的文件。

src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(972,19): error TS1005: ';' expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(974,10): error TS1128: Declaration or statement expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(975,17): error TS1005: ';' expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(976,17): error TS1005: ';' expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(977,24): error TS1005: ';' expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(978,17): error TS1005: ';' expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(986,4): error TS1128: Declaration or statement expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(987,24): error TS1005: ';' expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(988,20): error TS1005: ';' expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(989,13): error TS1005: ';' expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(990,13): error TS1005: ';' expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(991,25): error TS1005: ';' expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(992,15): error TS1005: ';' expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(993,21): error TS1005: ';' expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(994,20): error TS1005: ';' expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(996,6): error TS1128: Declaration or statement expected.
src/app/services/MYPROJECT.NG/src/app/services/api/rr.api.ts(997,14): error TS1005: ';' expected.

回答by vidu

I have faced this issue.This can be happened because of the mismatch between Angular version and TypeScript version.So simply follow below steps

我遇到过这个问题。这可能是由于 Angular 版本和 TypeScript 版本之间的不匹配而发生的。所以只需按照以下步骤操作

  1. Step 01 - Installing TypeScript for Visual Studio 2015

    • TypeScript for Visual Studio 2015
    • before install, check whether it is installed already(check the path C:\Program Files(x86)\Microsoft SDKs\TypeScript) and if it is there, then delete it.
    • then installed it and it will be in C:\Program Files (x86)\Microsoft SDKs\TypeScript\3.2
    • then set the path for TypeScript using path variable(set it as C:\Program Files (x86)\Microsoft SDKs\TypeScript\3.2)
  2. Step 02 - Installing latest Angular version(Angular 7) using following commands

    • npm install -g @angular/cli
    • npm install -g @angular/core
  3. Step 03 - Checking TypeScript version using following command

    • ng version
    • this will show typescript version, something like 3.2.4 (installed in previous step)

    • Specially, I like to thank to Shahriar Morshedand TranslucentCloudbecause I referred their answersto fix my issue.

  1. 步骤 01 - 为 Visual Studio 2015 安装 TypeScript

    • Visual Studio 2015 的打字稿
    • 在安装之前,检查是否已经安装(检查路径C:\Program Files(x86)\Microsoft SDKs\TypeScript),如果存在,则删除它。
    • 然后安装它,它将在 C:\Program Files (x86)\Microsoft SDKs\TypeScript\3.2
    • 然后使用路径变量设置 TypeScript 的路径(将其设置为 C:\Program Files (x86)\Microsoft SDKs\TypeScript\3.2)
  2. 步骤 02 - 使用以下命令安装最新的 Angular 版本(Angular 7)

    • npm install -g @angular/cli
    • npm install -g @angular/core
  3. 步骤 03 - 使用以下命令检查 TypeScript 版本

回答by vidu

Heyy @Kelli I got the same error, but I solved it. If you use visual studio then please copy your code in Visual Studio Code and select language as a typescript. And you getting red where you misspell after you indent your code and correctly add opening and closing brackets, add semicolons.

嘿@Kelli 我遇到了同样的错误,但我解决了。如果您使用 Visual Studio,请在 Visual Studio Code 中复制您的代码并选择语言作为打字稿。在缩进代码并正确添加左括号和右括号、添加分号后,您会在拼写错误的地方变红。

回答by Florin D

I spent a few hours today chasing a similar issue (my error was about a comma character) in a LESS pre-processor.

我今天花了几个小时在 LESS 预处理器中寻找类似的问题(我的错误是关于逗号字符)。

 error TS1005: ',' expected

Ended up that I had some backtick characters inside comments in my LESS files and they were confusing the pre-processor (maybe it is written in TypeScript -Angular 5 project- where the backtick has a special treatment)

最终我的 LESS 文件中的注释中有一些反引号字符,它们混淆了预处理器(也许它是用 TypeScript -Angular 5 项目编写的 - 其中反引号有特殊处理)