-
Notifications
You must be signed in to change notification settings - Fork 937
Compile time asserts #2367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compile time asserts #2367
Conversation
@@ -43,7 +43,7 @@ export function fail(failure: string): never { | |||
* Fails if the given assertion condition is false, throwing an Error with the | |||
* given message if it did. | |||
*/ | |||
export function assert(assertion: boolean, message: string): void { | |||
export function assert(assertion: boolean, message: string): asserts assertion { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AMAZING!!!
For your next trick you can go through and fix all the places I made people explicitly do:
if (foo instanceof Foo) {
// use foo as Foo
} else {
fail(...)
}
For the sake of type-narrowing! 😛
(just joking, but we should probably keep an eye towards cleaning those up over time)
const version = this.fromVersion(doc.found!.updateTime!); | ||
return new Document(key, version, {}, undefined, doc.found!, v => | ||
assertPresent(doc.found.name, 'doc.found.name'); | ||
assertPresent(doc.found.updateTime, 'doc.found.updateTime'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to punt, but could we magic up assertPresent() to get rid of the remaining !
usage below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated. Do you want to take another look?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
No description provided.