Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

verify if object is of a certain type type in typescript

export interface SuccessResponse<T> extends ResponseEntity {
    data: T;
}

export interface FailureResponse<T> extends ResponseEntity {
    error: T;
}

// type-guard
export function isSuccessResponseTypeGuard<T>(data: ResponseEntity): data is SuccessResponse<T> {
    return (<SuccessResponse<T>>data).data !== undefined;
}

// use
if (!isSuccessResponseTypeGuard<IUser>(result)) {
  throw new UnauthorizedException(result.message);
}
Comment

check type of object typescript

function isFish(pet: Fish | Bird): pet is Fish {
   return (<Fish>pet).swim !== undefined;
}

// Both calls to 'swim' and 'fly' are now okay.
if (isFish(pet)) {
  pet.swim();
}
else {
  pet.fly();
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: typescript combine interfaces 
Typescript :: simulate click typescript 
Typescript :: typescript pick type from interface 
Typescript :: string of bits to integer java 
Typescript :: create react app with redux and typescript 
Typescript :: typescript function as type 
Typescript :: cannot redeclare block-scoped variable typescript 
Typescript :: typescript datetimte 
Typescript :: python discord action when someone reacts to message 
Typescript :: add bullet points in text widget flutter 
Typescript :: angular initail valeur in fromgroup 
Typescript :: conda tsinghua 
Typescript :: cra ts pwa 
Typescript :: class validator array of enum 
Typescript :: preventing +,-,e from input ts 
Typescript :: module.exports mongodb connection 
Typescript :: angular find and remove from string 
Typescript :: typescript react function coponent props 
Typescript :: ts date get minutes 
Typescript :: golang check array index exists in slice 
Typescript :: Interface with custom property name type 
Typescript :: How to disable form control but keep value 
Typescript :: ts factory pattern 
Typescript :: open ports for remote access on linux 
Typescript :: how to show code conflicts in git 
Typescript :: laravel row exists or null 
Typescript :: rascal npm 
Typescript :: best way to display developer credits on a website 
Typescript :: excel separate input cell contents by space 
Typescript :: Carbohydrates and fats both 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =