25
loading...
This website collects cookies to deliver better user experience
npm i typescript -g
to install the typeScript compiler. Note this installs it globally on our machine..ts
extension. And compile it to JavaScript it would still get the job done.// Valid JavaScript is Typescript
const name = 'Bruce';
console.log(name)
// adding types
const name: string = 'Burger';
const price: number = 380;
const amount: number = 3
const instock: boolean = true
const burger = { name, price, amount, instock }
console.log(burger)
tsc filename.ts
. The typescript compiler will compile this code down to vanilla JavaScript for us and if you inspect it, you will find out that it reads to plain JavaScript.// Explicit Typing
let name: string;
let greetings: string
let age: number;
// Name can only store strings
name = 'sam'
// age can only store numbers
age = 24
// functions can also have types
let greet: (name:string) => string
greet = (name) => `hello ${name}`
// Implicit Typing
let hero = 'Thor'
let amount = 2500
greetings = greet('samson')
age = greet(23) // NOT POSSIBLE
console.log(greetings) // hello samson
let hero: any = 'Thor'
hero = { name: 'Thor'}
hero = true
// Union types
let hero: string | object;
hero = 'Thor'
hero = { name: 'Thor' }
// Restricting Values for variables
let universe: 'DCU' | 'MCU';
// universe can only be DCU or MCU
universe = 'DCU'
// Not Possible
universe = 'Something else';
type Hero = {
name: string,
universe: 'DCU' | 'MCU'
}
const superman: Hero = {
name: 'superman',
universe: 'DCU'
}
Interfaces
interface Hero {
name: string,
universe: 'DCU' | 'MCU'
}
const superman: Hero = {
name: 'superman',
universe: 'DCU'
}
interface Human {
name: string,
gender: string
}
interface Hero implements Human {
powers: string[],
uninverse: 'DCU' | 'MCU'
}
// ALl heroes must have the same
// properties as humans
powers
properties on a hero is an array, but we add the string type before the array, this tells TypeScript that the powers property is an array of strings, we can also have arrays of any type. If we want more control over what element is in what position in an array the we could use a tuple.// array of numbers;
let nums: number[];
let heroes = Hero[];
let mixedArr: string[] | number[];
nums = [1, 3, 5, 7] // Valid
mixedArr = ['sam', 200, 23, 'foo'] // Valid
nums = ['sam', false] // Not possible
mixedArr = [false, { hero: 'supes'}] // Not possible
// TUPLES
let tup : [string, hero, number];
tup = ['sam', superman, 23];
tsc ./file_name
, file_name should be the name of the file you are trying to compile to JavaScript. If there are no errors, TypeScript will sipt out a JavaScript version of your TypeScript code, even if there are certain bugs in your code it will still spit out the compiled code.tsconfig.json
file. The config file is the best way to do this because there is a handful of compiler options that we can customize.tsc --w
and this will compile your file in watch mode, anytime there is a change to the source code the TypeScript compiler will automatically compile the code again. To generate a blank config file run tsc --init
and TypeScript will give us a blank tsconfig.json
file. tsc --all
and it will print all compiler options in your command line . To throw of the version of TypeScript you have installed in your computer run tsc --version
. There is a handful of more CLI commands we can but discuss here and that fits into it's own article