Skip to content
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

[invariant] Assertion functions #40218

Closed
wants to merge 1 commit into from
Closed

[invariant] Assertion functions #40218

wants to merge 1 commit into from

Conversation

josh
Copy link

@josh josh commented Nov 7, 2019

Make use of TypeScript 3.7 assertion function feature to implement invariant.

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Add or edit tests to reflect the change. (Run with npm test.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

If changing an existing definition:

@typescript-bot
Copy link
Contributor

👋 Hi there! I’ve run some quick measurements against master and your PR. These metrics should help the humans reviewing this PR gauge whether it might negatively affect compile times or editor responsiveness for users who install these typings.

Let’s review the numbers, shall we?

Comparison details 📊
master #40218 diff
Batch compilation
Memory usage (MiB) 36.0 36.1 +0.3%
Type count 2080 2081 0%
Assignability cache size 103 103 0%
Language service
Samples taken 9 9 0%
Identifiers in tests 9 9 0%
getCompletionsAtPosition
    Mean duration (ms) 88.7 87.1 -1.8%
    Mean CV 21.0% 23.6%
    Worst duration (ms) 112.6 101.3 -10.0%
    Worst identifier invariant invariant
getQuickInfoAtPosition
    Mean duration (ms) 82.7 84.3 +1.9%
    Mean CV 19.8% 21.9% +10.5%
    Worst duration (ms) 96.0 98.2 +2.3%
    Worst identifier invariant invariant

It looks like nothing changed too much. I won’t post performance data again unless it gets worse.

@typescript-bot typescript-bot added the Perf: Same typescript-bot determined that this PR will not significantly impact compilation performance. label Nov 7, 2019
@typescript-bot typescript-bot added this to Waiting for Reviewers in Pull Request Status Board Nov 7, 2019
@typescript-bot typescript-bot added the Popular package This PR affects a popular package (as counted by NPM download counts). label Nov 7, 2019
@typescript-bot
Copy link
Contributor

typescript-bot commented Nov 7, 2019

@josh Thank you for submitting this PR!

🔔 @bennett000 @dtinth - please review this PR in the next few days. Be sure to explicitly select Approve or Request Changes in the GitHub UI so I know what's going on.

If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead.

@bennett000
Copy link
Contributor

Looks good to me.

That said I'm not a reviewer (on the right sidebar) and I cannot seem to approve as @typescript-bot suggests

Copy link
Contributor

@dtinth dtinth left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

I'm excited to use assertion functions in TypeScript as well. However I think we should retain support for invariant (false) pattern.

@@ -21,5 +21,5 @@ invariant('', 'Some other error');
// handles extra variables
invariant(true, 'Error, error, read all about it', 37, {}, 'hello');

// $ExpectType {}
// $ExpectType void | {}
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this should stay as {} to facilitate this pattern:

const thing = getThing() || invariant(false, 'Expected getThing to return something')

as well as

const thing = getThing() ?? invariant(false, 'Expected getThing to return something')

@@ -11,8 +13,7 @@ export as namespace invariant;

declare namespace invariant {
interface InvariantStatic {
(testValue:false, format?:string, ...extra:any[]):never;
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO we should keep this overload to not break existing test (see comment below).

@@ -11,8 +13,7 @@ export as namespace invariant;

declare namespace invariant {
interface InvariantStatic {
(testValue:false, format?:string, ...extra:any[]):never;
(testValue:any, format?:string, ...extra:any[]):void;
(testValue:any, format?:string, ...extra:any[]):asserts testValue;
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be great if some test can be added for this.

@typescript-bot typescript-bot moved this from Waiting for Reviewers to Needs Author Attention in Pull Request Status Board Nov 8, 2019
@typescript-bot typescript-bot added Revision needed This PR needs code changes before it can be merged. and removed Awaiting reviewer feedback labels Nov 8, 2019
@typescript-bot
Copy link
Contributor

@josh One or more reviewers has requested changes. Please address their comments. I'll be back once they sign off or you've pushed new commits or comments. Thank you!

@josh
Copy link
Author

josh commented Nov 9, 2019

I believe this should stay as {} to facilitate this pattern:

Is it possible for an asserts function to have a return value?

However I think we should retain support for invariant (false) pattern.

👍 that makes sense.

@typescript-bot
Copy link
Contributor

🔔 @dtinth - Thanks for your review of this PR! Can you please look at the new code and update your review status if appropriate?

Copy link
Contributor

@dtinth dtinth left a comment

Choose a reason for hiding this comment

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

@josh Thanks! Mind adding a corresponding test for this change? Maybe a test could look like this:

declare const x: any
invariant(typeof x === 'string')
const s = x
// $ExpectType string
x

@josh
Copy link
Author

josh commented Nov 9, 2019

Mind adding a corresponding test for this change? Maybe a test could look like this:

Tried that, but got:

expect  TypeScript@next expected type to be:
  string
got:
  any

@josh josh closed this Nov 19, 2019
Pull Request Status Board automation moved this from Needs Author Attention to Done Nov 19, 2019
@josh josh deleted the invariant/assertion-function branch November 19, 2019 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Perf: Same typescript-bot determined that this PR will not significantly impact compilation performance. Popular package This PR affects a popular package (as counted by NPM download counts). Revision needed This PR needs code changes before it can be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants