Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

typescript

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.

like image 420
Kelly Avatar asked Jan 26 '17 14:01

Kelly


2 Answers

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

like image 52
Airon Teixeira Avatar answered Oct 26 '22 17:10

Airon Teixeira


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.

like image 42
bug-a-lot Avatar answered Oct 26 '22 18:10

bug-a-lot