Skip to content

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

Merged
merged 5 commits into from
Dec 2, 2019
Merged

Compile time asserts #2367

merged 5 commits into from
Dec 2, 2019

Conversation

schmidt-sebastian
Copy link
Contributor

No description provided.

@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it!

Copy link
Contributor

@mikelehen mikelehen left a 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');
Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link
Contributor

@mikelehen mikelehen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@schmidt-sebastian schmidt-sebastian merged commit a547892 into master Dec 2, 2019
@hsubox76 hsubox76 added this to the 7.5.1 milestone Dec 12, 2019
@firebase firebase locked and limited conversation to collaborators Jan 2, 2020
@schmidt-sebastian schmidt-sebastian deleted the mrschmidt/asserts branch February 28, 2020 23:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants