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

Open
wants to merge 1 commit into
base: master
from

Conversation

@josh
Copy link

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:

@josh josh force-pushed the josh:invariant/assertion-function branch 3 times, most recently from 50ba3e6 to f663502 Nov 7, 2019
@typescript-bot

This comment has been minimized.

Copy link
Contributor

typescript-bot commented Nov 7, 2019

👋 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.

@josh josh force-pushed the josh:invariant/assertion-function branch from f663502 to f518d9c Nov 7, 2019
@typescript-bot typescript-bot added this to Waiting for Reviewers in Pull Request Status Board Nov 7, 2019
@typescript-bot

This comment has been minimized.

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

This comment has been minimized.

Copy link
Contributor

bennett000 commented Nov 7, 2019

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 left a comment

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 | {}

This comment has been minimized.

Copy link
@dtinth

dtinth Nov 8, 2019

Contributor

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;

This comment has been minimized.

Copy link
@dtinth

dtinth Nov 8, 2019

Contributor

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;

This comment has been minimized.

Copy link
@dtinth

dtinth Nov 8, 2019

Contributor

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

This comment has been minimized.

Copy link
Contributor

typescript-bot commented Nov 8, 2019

@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

This comment has been minimized.

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.

@josh josh force-pushed the josh:invariant/assertion-function branch from f518d9c to a21f47f Nov 9, 2019
@typescript-bot

This comment has been minimized.

Copy link
Contributor

typescript-bot commented Nov 9, 2019

🔔 @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 left a comment

@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

This comment has been minimized.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Pull Request Status Board
Needs Author Attention
4 participants
You can’t perform that action at this time.