Skip to content

Advanced

Abstract Classes

abstract class TakePhoto {
constructor(
public cameraMode: string,
public filter: string
){}
abstract getSepia(): void
getReelTime():number{
return 8;
}
}
class Instagram extends TakePhoto{
constructor(
public cameraMode: string,
public filter: string,
public burst: number
){
super(cameraMode, filter)
}
getSepia(): void {
console.log('sepia');
}
}
const hc = new Instagram("test", "Test", 3);
let reeltime = hc.getReelTime();
console.log(reeltime);

Detection

function detectType(val: number | string){
if(typeof val === "string"){
return val.toLowerCase();
}
return val + 3;
}
function provideId(id: string | null){
if(!id){
console.log("Please provide an ID");
return;
}
id.toLowerCase();
}
function printAll(strs: string | string[] | null){
if(strs){
if(typeof strs === "object"){
for(const s of strs){
console.log(s);
}
} else if(typeof strs === "string"){
console.log(strs);
}
}
}
interface User {
name: string,
email: string
}
interface Admin{
name: string,
email: string,
isAdmin: boolean,
}
function isAdminAccount(account:User | Admin){
if("isAdmin" in account){
return account.isAdmin;
}
}
function logValue(x: Date | string){
if(x instanceof Date){
console.log(x.toUTCString());
} else {
console.log(x.toUpperCase());
}
}
type Fish = {swim: () => void};
type Bird = {fly: () => void};
function isFish(pet: Fish | Bird):pet is Fish{
return (pet as Fish).swim !== undefined;
}
function getFood(pet: Fish | Bird){
if(isFish(pet)){
pet
return "fish food";
} else {
pet
return "Bird food";
}
}
interface Circle {
kind: "circle",
radius: number,
}
interface Square {
kind: "square",
side: number,
}
interface Reactangle {
kind: "reactangle",
length: number,
width: number
}
type Shape = Circle | Square | Reactangle;
function getTrueShape(shape: Shape){
if(shape.kind === "circle"){
return Math.PI * shape.radius ** 2;
}
}
function getArea(shape: Shape){
switch(shape.kind){
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.side * shape.side;
case "reactangle":
return shape.length * shape.width;
default:
const _defaultforshape: never = shape
return _defaultforshape;
}
}

Protected

// class User {
// public email: string;
// public name: string;
// readonly city: string = "Jaipur";
// constructor (email: string, name: string){
// this.email = email;
// this.name = name;
// }
// }
class User {
protected _courseCount = 1;
readonly city: string = "Jaipur";
constructor(
public email: string,
public name: string,
){
}
private deleteToken(){
console.log("Token deleted");
}
get getAppleEmail(): string{
return `apple${this.email}`;
}
get courseCount(): number {
return this._courseCount;
}
set courseCount(courseNum){
if(courseNum <= 1){
throw new Error("Course count should be more than 1");
}
this._courseCount = courseNum;
}
}
class SubUser extends User {
isFamily: boolean = true;
changeCourseCount(){
this._courseCount = 4;
}
}
const hitesh = new User("h@h.com", 'hitesh');
hitesh.name;

Generics

const score: Array<number> = [];
const names: Array<number> = [];
function identifyOne(val: boolean | number): boolean | number {
return val;
}
function identifyTwo(val: any): any{
return val;
}
function identifyThree<Type>(val: Type): Type{
return val;
}
function identifyFour<T>(val: T): T{
return val;
}
interface Bottle {
brand: string,
type: number
}
// identifyFour<Bottle>({});
function getSearchProducts<T>(products: T[]): T {
const myIndex = 3;
return products[myIndex];
}
function getMoreSearchProducts<T>(products: T[]): T {
const myIndex = 4;
return products[myIndex];
}
interface Database {
connection: string,
username: string,
password: string,
}
function anotherFunction<T, U extends Database>(valOne: T, valTwo: U):object {
return {
valOne,
valTwo
}
}
interface Quiz{
name: string,
type: string,
}
interface Course{
name: string,
author: string,
subject: string,
}
class Sellable<T>{
public cart: T[] = [];
addToCart(product: T){
this.cart.push(product);
}
}

Classes

interface TakePhoto {
cameraMode: string
filter: string
burst: number
}
interface Story {
createStory(): void
}
class Instagram implements TakePhoto {
constructor(
public cameraMode: string,
public filter: string,
public burst: number
){}
}
class YouTube implements TakePhoto, Story{
constructor(
public cameraMode: string,
public filter: string,
public burst: number,
public short: string,
){}
createStory(): void{
console.log("Story was created");
}
}