Answers for "typescript object is string"

1

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);
}
Posted by: Guest on January-04-2022
0

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();
}
Posted by: Guest on September-15-2021

Code answers related to "typescript object is string"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language