Types in Functions [duplicate]

Solution 1:

You need to compile your code first.

tsc app.ts

Then run it with

node app.js

Solution 2:

You are running the code with NodeJS. You aren't transpiling it from TypeScript to JavaScript, so you are trying to treat your TypeScript as if it were JavaScript.

That might be OK, since TypeScript is a superset of JavaScript. In this particular case, your code also happens to be valid JS.

The destructuring assignments you are trying to use are an ES6 feature that are only supported in NodeJS from 6.5 onwards.

You need to upgrade NodeJS to a newer version.

Alternatively, transpile the TypeScript to ES5 with tsc app.ts

Solution 3:

To make it even easier on yourself and run it one command, you can use the ts-node library. After installing to your project, you can just run the following:

ts-node app.ts