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
Conversation
👋 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 📊
It looks like nothing changed too much. I won’t post performance data again unless it gets worse. |
@josh Thank you for submitting this PR! 🔔 @bennett000 @dtinth - please review this PR in the next few days. Be sure to explicitly select If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead. |
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 |
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 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.
types/invariant/invariant-tests.ts
Outdated
@@ -21,5 +21,5 @@ invariant('', 'Some other error'); | |||
// handles extra variables | |||
invariant(true, 'Error, error, read all about it', 37, {}, 'hello'); | |||
|
|||
// $ExpectType {} | |||
// $ExpectType void | {} |
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.
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; |
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.
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; |
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.
Would be great if some test can be added for this.
@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! |
Is it possible for an
👍 that makes sense. |
🔔 @dtinth - Thanks for your review of this PR! Can you please look at the new code and update your review status if appropriate? |
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.
@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
Tried that, but got:
|
Make use of TypeScript 3.7 assertion function feature to implement
invariant
.Please fill in this template.
npm test
.)npm run lint package-name
(ortsc
if notslint.json
is present).If changing an existing definition: