TypeScript Type definition, examples for Beginners – React, Node

Lets look into basic types in typescript and its common usages with examples. We will be checking out the primitive type definitions in typescript likke number, string etc.

Type definitions for Primitive Types in TypeScript

//Primitive types

let age:number;
age =30;

let decimalValue:number;
decimalValue = 5.2;

let userName:string;
userName = 'smartcoder';

let isInstructor:boolean;
isInstructor = true;

For number objects we use :Number

for string objects we use :String

Type definitions for Function Parameters in TypeScript

function add(a:number, b:number){
    return a + b;
}

const result = add(2, 5);

console.log(result);

Type definitions for Complex types in TypeScript

Type definition for Array in TypeScript

let cars: string[];

cars = ['Bently', 'Audi']

Type definition for Object in TypeScript

let person: {
  name: string,
  age: number
}


person = {
  name: 'Raj'
  age: 25
}

Type definition for Array of Objects in TypeScript

let people: {
  name: string,
  age: number
}[];

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *