8

I'm new to Typescript and I'm still figuring my way out, I searched this problem everywhere otherwise I wont be asking this question. I have a SyntaxError at the interface name and don't know how to solve it. Here is my short code:

interface Cars {
    name:string
    model:string
    topSpeed:number
    colors:string[]
    speedPrint(carSpeed:number):void
}

class BMW implements Cars{
    name = 'BMW X6'
    model = 'S'
    topSpeed = 320
    colors = ['cobalt red','phantom blue','white']
    speedPrint(topSpeed:number):void{
        console.log(`My car top speed is ${topSpeed}`)
    }
}

and this is the error:

app.ts:1
interface Cars {
          ^^^^

SyntaxError: Unexpected identifier
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
1
  • You need to provide far more info. See my partial answer. Commented Jul 4, 2021 at 4:04

3 Answers 3

10

There is no Typescript syntax error in your code. You can see that it has no errors at all in the Typescript playground.

But from the stacktrace it appears that you are trying to execute the Typescript directly from Node.js as if it were Javascript. That would explain the error because Javascript does not support interfaces; it's a Typescript thing.

You can compile or transpile the *.ts files to *.js using a tool like tsc, esbuild, deno or bun.

Or you can run Typescript directly using tsx or ts-node. Node.js has recently (Node v22.6.0, 2024) added experimental support for running Typescript directly like this:

node --experimental-strip-types example.ts

I can't give you a more specific answer without you providing more info about your setup, the command you executed that produced that error, etc.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for explanation, I was trying to get the logs in the VS Code like the tutorial I`m following but I think I missed something there.
That was the first thing i thought of after reading your answer, but I couldn't because I don't have a 15 reputation yet.
you have to compile your typescript file first and then run the compiled js file. I am getting similar error and resolved by doing below: tsc hash-map.ts && node hash-map.js
7

I was facing the same error but I did the following and it works:

  1. Install ts by typing this command in the terminal npm install -g typescript
  2. After this type tsc filename.ts
  3. Then node ./file.js (not ts) and see what gonna happen on the terminal

Comments

2

I think instead of run node .ts you can run

  1. tsc .ts
  2. node .js Because when you run the first command, it will generate the equivalent javascript file corresponds to your typescript file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.