Last active
March 25, 2021 00:09
-
-
Save maslade/603a1dd0d976105ffb17c349da9ab12c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 mustBeUserRequest(request: IGenericRequest): asserts request is IGetUserRequest { | |
if (!maybeUserRequest.username || !maybeUserRequest.username.length > 0) { | |
throw new Error('Bad input!'); | |
} | |
} | |
function handler(request: IGenericRequest) { | |
mustBeUserRequest(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