13

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

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.

Edited to add years later: Wanted to make it clear, I did not realize I had two versions of typescript on my computer- one was installed with Visual Studio a while back, and it was using that. Once I switched to use the node.js command prompt like bug-a-lot suggested in their answer below, it used the correct version. Using a regular windows command prompt, I could get it to work by navigating to the folder where tsc is found. It then successfully compiled without any changes to the code.

Kelly
  • 945
  • 2
  • 18
  • 31
  • 1
    You better update your typescript version. The current version is `2.1.5` and you're using `1.0.3`. – Nitzan Tomer Jan 26 '17 at 14:37
  • why would npm have installed an old version? I just got into this two weeks ago. I am having trouble updating it. – Kelly Jan 26 '17 at 14:37
  • unless typescript got on my computer another way... ugh – Kelly Jan 26 '17 at 14:38
  • How did you install it? Maybe you've installed the old version a while ago (or something you're using did) globally. Then you might have installed the new version locally. Try `./node_modules/typescript/bin/tsc -v` – Nitzan Tomer Jan 26 '17 at 14:39
  • 1
    There is a missing semi-colon for the return statement in `createCardPicker`. `return function() { .... }` should be changed to `return function() { .... };`. Also, the variable declaration statement `let deck = { .... }` is missing a semi-colon. Should be `let deck = { .... };` – David Sherret Jan 26 '17 at 15:12
  • I added those semi-colons but it didn't help and just added yet another error saying: cardPicker.ts(11,6): error TS1005: ';' expected. I am still seeing about upgrading my ts version – Kelly Jan 26 '17 at 15:34
  • and to answer how I installed it, I downloaded node.js and npm and then I'm assuming I simply ran "npm install -g typescript". Being new to npm as well is not helping – Kelly Jan 26 '17 at 15:35
  • So it turns out I have two versions of typescript on my computer, one was installed with Visual Studio a while back, and it was using that. Once I switched to use the node.js command prompt like bug-a-lot suggested, it used the correct version. Using a regular windows command prompt, I could get it to work by navigating to the folder where tsc is found. It successfully compiles now without any changes to the code – Kelly Jan 26 '17 at 16:15

7 Answers7

4

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

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

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

Today tsc -v return Version 2.8.1

With newer version you do not will get ts1005

3

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

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.

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:

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 let which was introduced in TS 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.

bug-a-lot
  • 2,446
  • 1
  • 22
  • 27
  • using node.js command prompt fixed my problem because it used the correct version of typescript, located in my AppData\Roaming\npm\node_modules\typescript folder – Kelly Jan 26 '17 at 16:16
0

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.

0

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

 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)

Florin D
  • 1,610
  • 18
  • 17
0

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/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.
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • 1
    What does "_`src/app/services/` was nested inside `src/app/services/MYPROJECT`_" mean, precisely? I see it if your error list, but how did you get into that state? What does it represent? How did you fix it? – ruffin Mar 08 '21 at 16:57
0

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

  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 Morshed and TranslucentCloud because I referred their answers to fix my issue.

Vidu VDS
  • 62
  • 9
0

JSON may be missing curly braces.

In example, adding the two commented lines fixes the error.

const myObject =
[
  {
    'id': '123',
    'person': [
      {   // MISSING BRACE
        'name': 'SushiGuy',
        'age': '99',
      }   // MISSING BRACE
    ]
  }
];
SushiGuy
  • 1,573
  • 17
  • 20