5

The fastest way to start a typescript project (in 30 seconds)

 2 years ago
source link: https://www.mailslurp.com/blog/fastest-way-to-start-a-typescript-project/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
The fastest way to start a typescript project (in 30 seconds)

Fastest way to start a typescript project

Typescript sometimes has a reputation for being difficult to get started with. Modern tooling has really improved this situation and it is now very easy to create a new project and get up and running in no time.

Create a new project (in 30 seconds)

In a terminal on Mac OSX or Linux create a new directory and navigate to it using this command:

mkdir example && cd $_

Setup node

You need a package.json file to run a typescript project. Use npm init to create one:

npm init --yes

The output shows a newly created package.json file:

{
  "name": "example",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Install typescript

Next save typescript compiler and the loader ts-node as development dependencies.

npm i -D typescript ts-node

The shell output shows the installed dependencies (with your version instead)

+ [email protected]
+ [email protected]
added 14 packages from 45 contributors and audited 14 packages in 1.209s
found 0 vulnerabilities

Setup tsconfig

Next you can create a tsconfig.json file using typescript’s tsc --init command:

./node_modules/.bin/tsc --init

A success message is shown in the terminal:

message TS6071: Successfully created a tsconfig.json file.

Create a typescript file

Now we can create a simple typescript file called index.ts

echo 'console.log("Hi")' > index.ts

Run the script

You can run typescript using NodeJS with the ts-node/esm loader. Run your script like so:

node --loader ts-node/esm index.ts

The output shows our message:

Hi

Next steps

You can now write and run typescript files easily. To make run commands easier try adding the scripts to the package.json file:

{
    "scripts": {
        "start": "node --loader ts-node/esm index.ts"
    }
}

Then run your script using npm start.

Why use Typescript?

Typescript is a superset of standard Javascript that adds a powerful type system to the Javascript you already know. It is totally optionally but can dramatically reduce compile-time bugs and exceptions due to the information that type systems can provide to a compiler and the process of compiling the typescript into javascript before running it. This step provides an extra step of logic validation before running your javascript. TS-Node is a helpful tool for running the compilation step inside node itself so that you can skip the build stage. MailSlurp uses Typescript to create email testing APIs for NodeJS. Send and receive emails and attachments from test email accounts using MailSlurp for free!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK