cover
TypescriptJavaScript

Typescript

Cover
https://cdn.iconscout.com/icon/free/png-512/free-typescript-1-1175078.png?f=webp&w=256
Slug
Typescript
Published
Published
Date
Jan 17, 2024
Category
Typescript
JavaScript

Syntax

let variable_Name : type = value
let greeting: string = "Hello World";
any
not datatype
let hero; function gethero() { return "hero"; } hero = gethero();
Union
//type type StringOrNum = string | number;

Functions

function add(a: number, b: number): number { return a + b; } //perameters are number, return type is also number
const getHello = (s: string):string =>{ return "" }
Arrow function
const heros = ["thor", "spiderman", "ironman"] heros.map((hero:string):string => { return `hero is ${hero}` })
map
//never function throwError(errorMsg:string):never{ throw new Error(errorMsg); } function infiniteLoop():never{ while(true){ } }

objects

function createUser({name: string, age: number, isActive: boolean}): void {} createUser({name: "Hero", age: 10, isActive: true});
const User = { name: "Hero", age: 10, isActive: true } function getUser(user: typeof User):void{ console.log(user); }
function createCourse(): {title: string, price: number}{ return {title: "TS", price: 10}; }
Type Aliases
type User = { name: string; email: string; isActive: boolean } type Mystring = string function createUser(user: User):User{}
type Point = { x: number; y: number; }; // Exactly the same as the earlier example function printCoord(pt: Point) { console.log("The coordinate's x value is + pt.x); 11 console.log("The coordinate's y value is " + pt.y); } printCoord({ x: 100, y: 100 });

READONLY

type User = { readonly _id: string; name: string; }; let myUser: User = { _id: "123", name: "hero" }; myUser.name = "spiderman"; myUser._id = "456"; //XXXXXXX error because of readonly

? (optional)

type User = { name: string; creditcard?: number; }; let myUser: User = { name: "hero" //may or may not write cardno. };



type cardNumber = { cardNumber: number; } type cardDate = { cardDate: number; } type cardDetails = cardNumber & cardDate & { cvv : number };

Union ( | )

type User1 = { name: string; age: number; id: number; } type Admin = { username: string; id: number; } let shiv: User1 | Admin = { name: "hero", age: 10, id: 123 } shiv = { username: "hc", id: 12345 } function getDbId(id: number | string): void { if(typeof id === "string") { id.toUpperCase(); } else { id.toFixed(); } } getDbId(123); getDbId("123");

Array

//array let arr: number[] = [1,2,3,4,5]; let arr1: string[] = ["1","2","3","4","5"]; let arr2: (string | number)[] = ["1",2,"3",4,"5"]; let arr3: Array<string | number> = ["1",2,"3",4,"5"];

Tuple

//tuple - Fixed order let arr: [string, number,string] = ["1", 2, "3"]; arr[2] = 5 //you can change values in tuble arr.push(true) //you can break tuple rules just use function of array

Interface

interface UserR { readonly dbId: number; email: string; googleId?: string; startTrial: () => string; getCoupons?: (couponame:string) => number; } const hitesh: UserR = { dbId: 123, email: "hero@gmail.com", googleId: "123", startTrial: () => { return "trail started "; }, getCoupons: (name= "hitest") => { return 10; } } -------------------------------------------------------------------------------------- interface Person { getPermission: () => string; } interface Staff extends Person { getPermission: () => string[]; }
notion image

private/public

private a = "hi" public b = 'hello'

Classes & Constructor

class User2 { public email: string private name_: string readonly city: string = "Jaipur" constructor (email: string, name: string) { this.email = email; this.name = name } } const hit = new User2("h@h.com", "hitesh")
class Usr { readonly city: string = "Jaipur" constructor ( public email: string, public name: string, // private userId: string ) { } } const hiteh = new Usr("h@h.com", "hitesh")

Related Posts