Skip to content

Instantly share code, notes, and snippets.

@maslade
Created March 24, 2021 23:54
Show Gist options
  • Save maslade/d5863558026fca7f4add646cf2839fd2 to your computer and use it in GitHub Desktop.
Save maslade/d5863558026fca7f4add646cf2839fd2 to your computer and use it in GitHub Desktop.
// much better
interface IGenericRequest {
requestId: string;
}
interface IGetUserRequest extends IGenericRequest {
username: string;
}
function isUserRequest(request: IGenericRequest): request is IGetUserRequest {
const maybeUserRequest = request as IGetUserRequest;
return maybeUserRequest.username && maybeUserRequest.username.length > 0;
}
function handler(request: IGenericRequest) {
if (isUserRequest(request)) {
const username = request.username;
// ...the rest of the handler...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment