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 http get attach headers 
Typescript :: array in typescript 
Typescript :: content script matches all 
Typescript :: input fc typescript 
Typescript :: avatar image mui not centeered 
Typescript :: gamemanager unity resets after reloading scene 
Typescript :: create CSS class in directive angular 
Typescript :: cra ts pwa 
Typescript :: angular animation done event type typescript 
Typescript :: react native paper 
Typescript :: check type of object typescript 
Typescript :: ipywidgets hide widget 
Typescript :: two absolute elements are overlapping css help 
Typescript :: startswith multiple arguments python 
Typescript :: python ffmpeg convert ts to mp4 
Typescript :: mailbox exists c# 
Typescript :: literal types typescript 
Typescript :: typescript array contains string 
Typescript :: data type of stack in c 
Typescript :: what is hello world in typescript 
Typescript :: botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the ListObjects operation: Access Denied 
Typescript :: nest js get request response 
Typescript :: firebase typescript 
Typescript :: angular minus date 
Typescript :: undetermined number of arguments in function r 
Typescript :: uTorrent Default Download Folder - Linux 
Typescript :: how t make fireball roblox or lua 
Typescript :: Destructuring props in styled-components 
Typescript :: check if package exists inside the device adb 
Typescript :: take two inputs from user and add them using callback function 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =