Skip to content

Commit 8b8f90a

Browse files
authored
chore: replace anys with unknowns (#9626)
1 parent bb720d2 commit 8b8f90a

23 files changed

Lines changed: 92 additions & 77 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
### Chore & Maintenance
1010

11+
- `[*]` Replace `any`s with `unknown`s ([#9626](https://github.com/facebook/jest/pull/9626))
1112
- `[@jest/transform]` Expose type `CacheKeyOptions` for `getCacheKey` ([#9762](https://github.com/facebook/jest/pull/9762))
1213

1314
### Performance

packages/expect/src/matchers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const toStrictEqualTesters = [
6464
type ContainIterable =
6565
| Array<unknown>
6666
| Set<unknown>
67-
| NodeListOf<any>
67+
| NodeListOf<Node>
6868
| DOMTokenList
6969
| HTMLCollectionOf<any>;
7070

@@ -630,7 +630,7 @@ const matchers: MatchersObject = {
630630

631631
if (
632632
typeof received !== 'string' &&
633-
(!received || typeof received.length !== 'number')
633+
(typeof received !== 'object' || typeof received.length !== 'number')
634634
) {
635635
throw new Error(
636636
matcherErrorMessage(

packages/expect/src/toThrowMatchers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ const toThrowExpectedObject = (
223223
matcherName: string,
224224
options: MatcherHintOptions,
225225
thrown: Thrown | null,
226-
expected: any,
226+
expected: Error,
227227
): SyncExpectationResult => {
228228
const pass = thrown !== null && thrown.message === expected.message;
229229

packages/jest-cli/src/cli/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,15 @@ export const buildArgv = (maybeArgv?: Array<string>): Config.Argv => {
7474
);
7575

7676
// strip dashed args
77-
return Object.keys(argv).reduce((result, key) => {
78-
if (!key.includes('-')) {
79-
result[key] = argv[key];
80-
}
81-
return result;
82-
}, {} as Config.Argv);
77+
return Object.keys(argv).reduce<Config.Argv>(
78+
(result, key) => {
79+
if (!key.includes('-')) {
80+
result[key] = argv[key];
81+
}
82+
return result;
83+
},
84+
{$0: argv.$0, _: argv._},
85+
);
8386
};
8487

8588
const getProjectListFromCLIArgs = (

packages/jest-config/src/ReporterValidationErrors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function createArrayReporterError(
3939
arrayReporter: Config.ReporterConfig,
4040
reporterIndex: number,
4141
valueIndex: number,
42-
value: string | Record<string, any>,
42+
value: string | Record<string, unknown>,
4343
expectedType: string,
4444
valueName: string,
4545
): ValidationError {

packages/jest-config/src/normalize.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,14 +746,14 @@ export default function normalize(
746746
? _replaceRootDirTags(options.rootDir, project)
747747
: project,
748748
)
749-
.reduce((projects, project) => {
749+
.reduce<Array<string>>((projects, project) => {
750750
// Project can be specified as globs. If a glob matches any files,
751751
// We expand it to these paths. If not, we keep the original path
752752
// for the future resolution.
753753
const globMatches =
754754
typeof project === 'string' ? glob(project) : [];
755755
return projects.concat(globMatches.length ? globMatches : project);
756-
}, [] as Array<string>);
756+
}, []);
757757
break;
758758
case 'moduleDirectories':
759759
case 'testMatch':

packages/jest-core/src/FailedTestsCache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ export default class FailedTestsCache {
2929
.reduce<TestMap>((suiteMap, testResult) => {
3030
suiteMap[testResult.testFilePath] = testResult.testResults
3131
.filter(test => test.status === 'failed')
32-
.reduce((testMap, test) => {
32+
.reduce<{[name: string]: true}>((testMap, test) => {
3333
testMap[test.fullName] = true;
3434
return testMap;
35-
}, {} as {[name: string]: true});
35+
}, {});
3636
return suiteMap;
3737
}, {});
3838

packages/jest-core/src/watch.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,11 @@ export default function watch(
158158
if (globalConfig.watchPlugins != null) {
159159
const watchPluginKeys: WatchPluginKeysMap = new Map();
160160
for (const plugin of watchPlugins) {
161-
const reservedInfo =
162-
RESERVED_KEY_PLUGINS.get(plugin.constructor as WatchPluginClass) ||
163-
({} as ReservedInfo);
161+
const reservedInfo: Pick<
162+
ReservedInfo,
163+
'forbiddenOverwriteMessage' | 'key'
164+
> =
165+
RESERVED_KEY_PLUGINS.get(plugin.constructor as WatchPluginClass) || {};
164166
const key = reservedInfo.key || getPluginKey(plugin, globalConfig);
165167
if (!key) {
166168
continue;

packages/jest-docblock/src/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const ltrimNewlineRe = /^(\r?\n)+/;
1818
const multilineRe = /(?:^|\r?\n) *(@[^\r\n]*?) *\r?\n *(?![^@\r\n]*\/\/[^]*)([^@\r\n\s][^@\r\n]+?) *\r?\n/g;
1919
const propertyRe = /(?:^|\r?\n) *@(\S+) *([^\r\n]*)/g;
2020
const stringStartRe = /(\r?\n|^) *\* ?/g;
21+
const STRING_ARRAY: ReadonlyArray<string> = [];
2122

2223
export function extract(contents: string): string {
2324
const match = contents.match(docblockRe);
@@ -65,10 +66,7 @@ export function parseWithComments(
6566
typeof result[match[1]] === 'string' ||
6667
Array.isArray(result[match[1]])
6768
) {
68-
result[match[1]] = ([] as Array<string>).concat(
69-
result[match[1]],
70-
nextPragma,
71-
);
69+
result[match[1]] = STRING_ARRAY.concat(result[match[1]], nextPragma);
7270
} else {
7371
result[match[1]] = nextPragma;
7472
}
@@ -123,7 +121,7 @@ export function print({
123121
}
124122

125123
function printKeyValues(key: string, valueOrArray: string | Array<string>) {
126-
return ([] as Array<string>)
127-
.concat(valueOrArray)
128-
.map(value => `@${key} ${value}`.trim());
124+
return STRING_ARRAY.concat(valueOrArray).map(value =>
125+
`@${key} ${value}`.trim(),
126+
);
129127
}

packages/jest-fake-timers/src/jestFakeTimers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ export default class FakeTimers<TimerRef> {
372372
const promisifiableFakeSetTimeout = fn(this._fakeSetTimeout.bind(this));
373373
promisifiableFakeSetTimeout[util.promisify.custom] = (
374374
delay?: number,
375-
arg?: any,
375+
arg?: unknown,
376376
) =>
377377
new Promise(resolve => promisifiableFakeSetTimeout(resolve, delay, arg));
378378

0 commit comments

Comments
 (0)