Typescript Quick Introduction

Installation

npm install -g typescript ts-node
tsc -v 

Types

String

let language : string = "Javascript";
let language : string = "";
let language : string = 'Javascript';

Number

let sum : number = 0;
let sum : number = -5;
let sum : number = 0.7;
let big: bigint = 100n;

Boolean

let isApproved : boolean = false;
let isApproved : boolean = true;

Symbol

let sym1 : Symbol = Symbol();
let sym2 : Symbol = Symbol("key");

Functions

function add(a:number, b:number) : number {
return a + b;
}
function (a:number, b:number) : number {
return a + b;
}
const add = (firstName: string, lastName: string) : string => {
return firstName + ' ' + lastName;
}
const logger = (a:number) : void => {
console.log(a);
}

Object Destruction

const {foo}: {foo: string} = bar;

Array Destruction

function f([a,b,c]: [number, string, number]) {

}

Array

let nums:number[] = [1,2,3,3]
let multi:number[][] = [[1,2,3],[23,24,25]]
// or
let list: Array<number> = [1, 2, 3];

Class

class A {}

function create(ctor: { new(): A }): A {
return new ctor();
}

let a = create(A);

Any

let result : any;
let getUser = (id : number) : any => {
return this.db.getUser(id);
}

Interface

interface User{
id: number,
name: string
}
const user = res.data as User;
console.log(user.name);

Using Multiple Types

let getUser = (id : number) : (User | null) => {
return this.db.getUser(id);
}

Final

Source

--

--

--

Software Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Vuex: State management tool for Vue.js

ES8 What’s New in JavaScript?

How to set up functional testing for a web app from scratch in 5 minutes

JavaScript Objects

Add Survey Creator / Form Builder to your React application

Killer JavaScript One-Liners ☝️

Introducing BLoC Pattern with React and RxJS

AEM DAM images not able to map from AEM to React in CSS / SASS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cem Karakurt

Cem Karakurt

Software Developer

More from Medium

Complete Dive Into TypeScript: Part 1

How JavaScript works: the factory design pattern + 4 use cases

Using async/await in a forEach loop (you can’t)

Abstractions in Action: ORM’s and Javascript