chore: replace anys with unknowns#9626
chore: replace anys with unknowns#9626SimenB merged 9 commits intojestjs:masterfrom G-Rath:replace-anys-with-unknowns
anys with unknowns#9626Conversation
| const multilineRe = /(?:^|\r?\n) *(@[^\r\n]*?) *\r?\n *(?![^@\r\n]*\/\/[^]*)([^@\r\n\s][^@\r\n]+?) *\r?\n/g; | ||
| const propertyRe = /(?:^|\r?\n) *@(\S+) *([^\r\n]*)/g; | ||
| const stringStartRe = /(\r?\n|^) *\* ?/g; | ||
| const STRING_ARRAY: Array<string> = []; |
There was a problem hiding this comment.
This in theory is more performant as it's not allocating a new array every time, but mainly it just lets us drop as Array<string> in two places
| name === 'NamedNodeMap' | ||
| ? Array.prototype.reduce.call(collection, propsReducer, {}) | ||
| isNamedNodeMap(collection) | ||
| ? Array.from(collection).reduce<Record<string, string>>( |
There was a problem hiding this comment.
I inlined this function as there's no way to pass a generic parameter otherwise; it's inlined in DOMElement, and so now actually matches 1:1.
| props[attribute.name] = attribute.value; | ||
| return props; | ||
| }, {} as any), | ||
| ? {} |
There was a problem hiding this comment.
This could actually be classed as a bug - it might not ever be reachable due to surrounding code, but still - it was an array when it should have been an object 🤷♂
Codecov Report
@@ Coverage Diff @@
## master #9626 +/- ##
=======================================
Coverage 64.90% 64.90%
=======================================
Files 288 288
Lines 12195 12197 +2
Branches 3022 3022
=======================================
+ Hits 7915 7917 +2
Misses 3639 3639
Partials 641 641
Continue to review full report at Codecov.
|
|
ci/circleci: test-browser:
😬 |
|
Failing test seems to be unrelated - it's failing on a different worker related test on each action |
| toBeInstanceOf(this: MatcherState, received: any, expected: Function) { | ||
| toBeInstanceOf( | ||
| this: MatcherState, | ||
| received: new () => unknown, |
There was a problem hiding this comment.
I don't think these are safe, I'd prefer to use unknown and narrow down. We're supposed to throw if they are called with the wrong type anyways, and making those checks narrow the type seems ideal. These get called by user code who provides whatever they feel like.
There was a problem hiding this comment.
Yeah you're totally right!
| }; | ||
|
|
||
| function isSpy(putativeSpy: any) { | ||
| function isSpy(putativeSpy: Spy) { |
There was a problem hiding this comment.
this is supposed to be a typeguard, so maybe
| function isSpy(putativeSpy: Spy) { | |
| function isSpy(putativeSpy: unknown): putativeSpy is Spy { |
?
SimenB
left a comment
There was a problem hiding this comment.
Thanks! Sorry about the slow response, been swamped lately.
I think most of these changes are great, but the changes in expect are not necessarily right I think (although they're not less right than any, but any is more honest)
…pshots * upstream/master: (225 commits) docs: add CLA link to contributing docs (jestjs#9789) chore: roll new version of docs v25.3.0 chore: update changelog for release chore(jest-types): correct type testRegex for ProjectConfig (jestjs#9780) feat(circus): enable writing async test event handlers (jestjs#9397) feat: enable all babel syntax plugins (jestjs#9774) chore: add helper for getting Jest's config in e2e tests (jestjs#9770) feat: pass ESM options to transformers (jestjs#9597) chore: replace `any`s with `unknown`s (jestjs#9626) feat: pass ESM options to Babel (jestjs#9766) chore(website): add copy button the code blocks (jestjs#9750) chore: bump istanbul-reports for new uncovered lines design (jestjs#9758) chore: correct CHANGELOG.md (jestjs#9763) chore(jest-types): expose type `CacheKeyOptions` for `getCacheK… (jestjs#9762) docs: Fix simple typo, seperated -> separated (jestjs#9760) v25.2.7 chore: update changelog for release fix: drop getters and setters when diffing objects for error (jestjs#9757) chore(jest-types): correct return type of shouldRunTestSuite fo… (jestjs#9753) ...
|
Note: this breaks a couple of tests in expect.addSnapshotSerializer({
print: (value: {}) => '',
test: (value: {}) => value === value,
});
expect.addSnapshotSerializer({
print: (value: {}, serialize: (val: {}) => string, indent: (str: string) => string, opts: {}) => '',
test: (value: {}) => value === value,
});
expect.addSnapshotSerializer({
print: (value: unknown) => '',
test: (value: {}) => value === value,
});
expect.addSnapshotSerializer({
print: (value: unknown, serialize: (val: {}) => string, indent: (str: string) => string, opts: {}) => '',
test: (value: {}) => value === value,
});No idea if this example is realistic or not, I just noticed it while running the TS team's Definitely Typed tests. It seems reasonable that a serialiser should handle any JS value, including |
|
I think it makes sense. Ideally |
if it doesn't then the type should be
If I understand correctly: I can't find the actual places in code where this is used, but that should do the trick if you're just doing: |
|
Oh also rereading @sandersn's example I think while yes the type is more accurate now, the i.e |
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
This replaces some of the
anytypes in the codebase with eitherunknownor a more accurate type.Also removes some casts in favor of using the generic parameter when calling `reduce.
Test plan
Make a PR, and see if it blows up :D