TypeScript Quick Start Guide
This article will show you how to quickly get up and running with TypeScript, and we will discuss the pros and cons of using TS. TypeScript is a superset of JavaScript and the TC39 standard. With TS, We are able to define both simple and complex static types for the variables in our code.
TypeScript is simply JavaScript with the optional addition of types for your variables. You can write in modern JavaScript syntax (ES2015+) when coding in TypeScript, and the compiler will convert the TypeScript (.ts
) into JavaScript (.js
) as well as offering the ability to transpile the JavaScript to previous versions such as ES5.
Note that if you are using module resolution such as
require()
orimport/export
for the browser, you will still need to use a bundler for the browser like Webpack, Rollup, or SystemJS.
Installing TypeScript
First install TypeScript using either npm:
npm install -g typescript
or yarn:
yarn global add typescript
Once installed globally, you will have the tsc
command available in your terminal.
Compiling a .ts File
Using your terminal, create a new directory called ts-simple
with the following command:
mkdir ts-simple
cd
into this directory and create an index.ts
file. Inside this file, we will create a function called sayHello
with an argument name
which is of type string
.
Now use tsc
to compile your index.ts
:
tsc index.ts
This creates an index.js
which has removed the typing from the sayHello
function parameter as well as converted the file into ES5 code. The resulting JS file can be safely run on the browser or in Node. Your JavaScript file will look like…