-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
feat(testlab): improve typings for toJSON()
helper
#3283
Conversation
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.
Why use Math.random()
?
@@ -62,6 +62,50 @@ describe('toJSON', () => { | |||
expectUndefined(value); | |||
}); | |||
|
|||
it('handles `object | null`', () => { | |||
const input: object | null = Math.random() ? {} : null; |
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.
Wouldn't this always yield {}
?
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.
The Math.random() function returns a floating-point, pseudo-random number in the range 0–1 (inclusive of 0, but not 1) with approximately uniform distribution over that range
I think Math.random() ?
returns true
in most cases.
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.
Yes, so the behavior of input = null
will rarely get tested.
}); | ||
|
||
it('handles `object | undefined`', () => { | ||
const input: object | undefined = Math.random() ? {} : undefined; |
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.
Please extract Math.random()
? to a helper function, such as: randomChoice(...values: any[]): any
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.
For example:
function randomChoice(...values: any[]): any {
if (!values.length) return undefined;
const index = Math.floor(Math.random() * values.length);
return values[index];
}
Thank you @raymondfeng and @hacksparrow for your comments. I think I really need to improve the tests to make it more clear what is going on and what is being tested. In the tests, I don't really care about the actual value being converted via |
The main problem is that from what I can tell by observing type hints offered by VSCode, the following statement is optimized by TypeScript and a simpler type is used instead. const input: object | null = {};
const output = toJSON(input);
// VSCode shows the following hint:
// (alias) toJSON(value: object): object (+14 overloads)
// import toJSON Compare to my current version: const input: object | null = Math.random() ? {} : null;
const output = toJSON(input);
// VSCode shows the following hint:
// (alias) toJSON(value: object | null): object | null (+14 overloads)
// import toJSON |
6b25603
to
04c4307
Compare
@raymondfeng @hacksparrow updated, LGTY now? |
LGTM if it's just for compile-time TS checks. |
Let the compiler understand toJSON's behavior for commonly used union types like `object | null` or `unknown[] | null`. Signed-off-by: Miroslav Bajtoš <[email protected]>
04c4307
to
7bd2264
Compare
While working on #2634, I discovered that
findOne
returnsT | null
and this type cannot be passed totoJSON
helper, because there is no overload accepting such type.This pull requests improves the type definitions to let the compiler understand toJSON's behavior for commonly used union types like
object | null
orunknown[] | null
.Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated👉 Check out how to submit a PR 👈