Created
March 24, 2021 23:54
-
-
Save maslade/d5863558026fca7f4add646cf2839fd2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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