Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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);
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #verify #object #type #type #typescript
ADD COMMENT
Topic
Name
9+4 =