Skip to content

Commit ccea646

Browse files
JoshRosensteinSimenB
authored andcommitted
General Type Maintenance (#8462)
1 parent e224f2d commit ccea646

File tree

44 files changed

+126
-98
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+126
-98
lines changed

packages/babel-plugin-jest-hoist/src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ const IDVisitor = {
8888
blacklist: ['TypeAnnotation', 'TSTypeAnnotation', 'TSTypeReference'],
8989
};
9090

91-
const FUNCTIONS: {
92-
[key: string]: (args: Array<NodePath>) => boolean;
93-
} = Object.create(null);
91+
const FUNCTIONS: Record<
92+
string,
93+
(args: Array<NodePath>) => boolean
94+
> = Object.create(null);
9495

9596
FUNCTIONS.mock = (args: Array<NodePath>) => {
9697
if (args.length === 1) {

packages/expect/src/asymmetricMatchers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ class ObjectContaining extends AsymmetricMatcher<Record<string, any>> {
158158
for (const property in this.sample) {
159159
if (
160160
hasProperty(other, property) &&
161-
equals((this.sample as any)[property], other[property]) &&
162-
!emptyObject((this.sample as any)[property]) &&
161+
equals(this.sample[property], other[property]) &&
162+
!emptyObject(this.sample[property]) &&
163163
!emptyObject(other[property])
164164
) {
165165
return false;
@@ -171,7 +171,7 @@ class ObjectContaining extends AsymmetricMatcher<Record<string, any>> {
171171
for (const property in this.sample) {
172172
if (
173173
!hasProperty(other, property) ||
174-
!equals((this.sample as any)[property], other[property])
174+
!equals(this.sample[property], other[property])
175175
) {
176176
return false;
177177
}

packages/expect/src/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ const makeThrowingMatcher = (
299299

300300
const handlError = (error: Error) => {
301301
if (
302-
(matcher as any)[INTERNAL_MATCHER_FLAG] === true &&
302+
matcher[INTERNAL_MATCHER_FLAG] === true &&
303303
!(error instanceof JestAssertionError) &&
304304
error.name !== 'PrettyFormatPluginError' &&
305305
// Guard for some environments (browsers) that do not support this feature.
@@ -314,10 +314,7 @@ const makeThrowingMatcher = (
314314
let potentialResult: ExpectationResult;
315315

316316
try {
317-
potentialResult = matcher.apply(
318-
matcherContext,
319-
([actual] as any).concat(args),
320-
);
317+
potentialResult = matcher.call(matcherContext, actual, ...args);
321318

322319
if (isPromise(potentialResult)) {
323320
const asyncResult = potentialResult as AsyncExpectationResult;
@@ -340,7 +337,7 @@ const makeThrowingMatcher = (
340337
};
341338

342339
expect.extend = (matchers: MatchersObject): void =>
343-
setMatchers(matchers, false, expect as any);
340+
setMatchers(matchers, false, expect);
344341

345342
expect.anything = anything;
346343
expect.any = any;

packages/expect/src/jasmineUtils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ function keys(
208208
return keys.concat(
209209
(Object.getOwnPropertySymbols(o) as Array<any>).filter(
210210
symbol =>
211-
(Object.getOwnPropertyDescriptor(o, symbol) as any).enumerable,
211+
(Object.getOwnPropertyDescriptor(o, symbol) as PropertyDescriptor)
212+
.enumerable,
212213
),
213214
);
214215
})(obj);

packages/expect/src/jestMatchersObject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const JEST_MATCHERS_OBJECT = Symbol.for('$$jest-matchers-object');
1717
// Jest may override the stack trace of Errors thrown by internal matchers.
1818
export const INTERNAL_MATCHER_FLAG = Symbol.for('$$jest-internal-matcher');
1919

20-
if (!(global as any)[JEST_MATCHERS_OBJECT]) {
20+
if (!global.hasOwnProperty(JEST_MATCHERS_OBJECT)) {
2121
Object.defineProperty(global, JEST_MATCHERS_OBJECT, {
2222
value: {
2323
matchers: Object.create(null),

packages/expect/src/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import {Config} from '@jest/types';
99
import * as jestMatcherUtils from 'jest-matcher-utils';
10+
import {INTERNAL_MATCHER_FLAG} from './jestMatchersObject';
1011

1112
export type SyncExpectationResult = {
1213
pass: boolean;
@@ -17,11 +18,10 @@ export type AsyncExpectationResult = Promise<SyncExpectationResult>;
1718

1819
export type ExpectationResult = SyncExpectationResult | AsyncExpectationResult;
1920

20-
export type RawMatcherFn = (
21-
received: any,
22-
expected: any,
23-
options?: any,
24-
) => ExpectationResult;
21+
export type RawMatcherFn = {
22+
(received: any, expected: any, options?: any): ExpectationResult;
23+
[INTERNAL_MATCHER_FLAG]?: boolean;
24+
};
2525

2626
export type ThrowingMatcherFn = (actual: any) => void;
2727
export type PromiseMatcherFn = (actual: any) => Promise<void>;

packages/expect/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const hasOwnProperty = (object: object, key: string) =>
4949
hasGetterFromConstructor(object, key);
5050

5151
export const getPath = (
52-
object: object,
52+
object: Record<string, any>,
5353
propertyPath: string | Array<string>,
5454
): GetPath => {
5555
if (!Array.isArray(propertyPath)) {
@@ -59,7 +59,7 @@ export const getPath = (
5959
if (propertyPath.length) {
6060
const lastProp = propertyPath.length === 1;
6161
const prop = propertyPath[0];
62-
const newObject = (object as any)[prop];
62+
const newObject = object[prop];
6363

6464
if (!lastProp && (newObject === null || newObject === undefined)) {
6565
// This is not the last prop in the chain. If we keep recursing it will

packages/jest-circus/src/formatNodeAssertErrors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ interface AssertionErrorWithStack extends AssertionError {
2020
stack: string;
2121
}
2222

23-
const assertOperatorsMap: {[key: string]: string} = {
23+
const assertOperatorsMap: Record<string, string> = {
2424
'!=': 'notEqual',
2525
'!==': 'notStrictEqual',
2626
'==': 'equal',
2727
'===': 'strictEqual',
2828
};
2929

30-
const humanReadableOperators: {[key: string]: string} = {
30+
const humanReadableOperators: Record<string, string> = {
3131
deepEqual: 'to deeply equal',
3232
deepStrictEqual: 'to deeply and strictly equal',
3333
equal: 'to be equal',

packages/jest-cli/src/init/generate_config_file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const stringifyOption = (
3131
);
3232
};
3333

34-
const generateConfigFile = (results: {[key: string]: unknown}): string => {
34+
const generateConfigFile = (results: Record<string, unknown>): string => {
3535
const {coverage, clearMocks, environment} = results;
3636

3737
const overrides: Record<string, any> = {};

packages/jest-cli/src/init/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ import {Config} from '@jest/types';
99

1010
export type ProjectPackageJson = {
1111
jest?: Partial<Config.InitialOptions>;
12-
scripts?: {[key: string]: string};
12+
scripts?: Record<string, string>;
1313
};

packages/jest-config/src/setFromArgv.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default function setFromArgv(
1616
): Config.InitialOptions {
1717
const argvToOptions = Object.keys(argv)
1818
.filter(key => argv[key] !== undefined && specialArgs.indexOf(key) === -1)
19-
.reduce((options: {[key: string]: unknown}, key) => {
19+
.reduce((options: Record<string, unknown>, key) => {
2020
switch (key) {
2121
case 'coverage':
2222
options.collectCoverage = argv[key];

packages/jest-config/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const _replaceRootDirInObject = <T extends ReplaceRootDirConfigObj>(
8484
};
8585

8686
type OrArray<T> = T | Array<T>;
87-
type ReplaceRootDirConfigObj = {[key: string]: Config.Path};
87+
type ReplaceRootDirConfigObj = Record<string, Config.Path>;
8888
type ReplaceRootDirConfigValues =
8989
| OrArray<ReplaceRootDirConfigObj>
9090
| OrArray<RegExp>

packages/jest-core/src/FailedTestsCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {Test} from 'jest-runner';
99
import {Config} from '@jest/types';
1010
import {TestResult} from '@jest/test-result';
1111

12-
type TestMap = {[key: string]: {[key: string]: boolean}};
12+
type TestMap = Record<string, Record<string, boolean>>;
1313

1414
export default class FailedTestsCache {
1515
private _enabledTestsMap?: TestMap;

packages/jest-core/src/TestScheduler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export default class TestScheduler {
217217
}
218218

219219
private _partitionTests(
220-
testRunners: {[key: string]: TestRunner},
220+
testRunners: Record<string, TestRunner>,
221221
tests: Array<Test>,
222222
) {
223223
if (Object.keys(testRunners).length > 1) {
@@ -327,7 +327,7 @@ export default class TestScheduler {
327327
*/
328328
private _getReporterProps(
329329
reporter: string | Config.ReporterConfig,
330-
): {path: string; options: {[key: string]: unknown}} {
330+
): {path: string; options: Record<string, unknown>} {
331331
if (typeof reporter === 'string') {
332332
return {options: this._options, path: reporter};
333333
} else if (Array.isArray(reporter)) {

packages/jest-core/src/getNoTestFoundVerbose.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default function getNoTestFoundVerbose(
1717
if (key === 'roots' && config.roots.length === 1) {
1818
return null;
1919
}
20-
const value = (config as {[key: string]: unknown})[key];
20+
const value = (config as Record<string, unknown>)[key];
2121
if (value) {
2222
const valueAsString = Array.isArray(value)
2323
? value.join(', ')

packages/jest-docblock/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import {EOL} from 'os';
99
import detectNewline from 'detect-newline';
1010

11-
type Pragmas = {[key: string]: string | Array<string>};
11+
type Pragmas = Record<string, string | Array<string>>;
1212

1313
const commentEndRe = /\*\/$/;
1414
const commentStartRe = /^\/\*\*/;

packages/jest-each/src/table/template.ts

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {isPrimitive} from 'jest-get-type';
1111
import {Global} from '@jest/types';
1212
import {EachTests} from '../bind';
1313

14-
type Template = {[key: string]: unknown};
14+
type Template = Record<string, unknown>;
1515
type Templates = Array<Template>;
1616
type Headings = Array<string>;
1717

@@ -70,11 +70,46 @@ const replaceKeyPathWithValue = (template: Template) => (
7070
return title.replace(match, pretty(value, {maxDepth: 1, min: true}));
7171
};
7272

73-
const getPath = (
74-
template: Template | any,
73+
/* eslint import/export: 0*/
74+
export function getPath<
75+
Obj extends Template,
76+
A extends keyof Obj,
77+
B extends keyof Obj[A],
78+
C extends keyof Obj[A][B],
79+
D extends keyof Obj[A][B][C],
80+
E extends keyof Obj[A][B][C][D]
81+
>(obj: Obj, path: [A, B, C, D, E]): Obj[A][B][C][D][E];
82+
export function getPath<
83+
Obj extends Template,
84+
A extends keyof Obj,
85+
B extends keyof Obj[A],
86+
C extends keyof Obj[A][B],
87+
D extends keyof Obj[A][B][C]
88+
>(obj: Obj, path: [A, B, C, D]): Obj[A][B][C][D];
89+
export function getPath<
90+
Obj extends Template,
91+
A extends keyof Obj,
92+
B extends keyof Obj[A],
93+
C extends keyof Obj[A][B]
94+
>(obj: Obj, path: [A, B, C]): Obj[A][B][C];
95+
export function getPath<
96+
Obj extends Template,
97+
A extends keyof Obj,
98+
B extends keyof Obj[A]
99+
>(obj: Obj, path: [A, B]): Obj[A][B];
100+
export function getPath<Obj extends Template, A extends keyof Obj>(
101+
obj: Obj,
102+
path: [A],
103+
): Obj[A];
104+
export function getPath<Obj extends Template>(
105+
obj: Obj,
106+
path: Array<string>,
107+
): unknown;
108+
export function getPath(
109+
template: Template,
75110
[head, ...tail]: Array<string>,
76-
): any => {
111+
): unknown {
77112
if (!head || !template.hasOwnProperty || !template.hasOwnProperty(head))
78113
return template;
79-
return getPath(template[head], tail);
80-
};
114+
return getPath(template[head] as Template, tail);
115+
}

packages/jest-environment/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type JestMockSpyOn = typeof jestMock.spyOn;
1818
// passed, or not. The context itself is optional, not properties within it.
1919
export type EnvironmentContext = Partial<{
2020
console: Console;
21-
docblockPragmas: {[key: string]: string | Array<string>};
21+
docblockPragmas: Record<string, string | Array<string>>;
2222
testPath: Config.Path;
2323
}>;
2424

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ const setGlobal = (
5353
};
5454

5555
export default class FakeTimers<TimerRef> {
56-
private _cancelledImmediates!: {[key: string]: boolean};
57-
private _cancelledTicks!: {[key: string]: boolean};
56+
private _cancelledImmediates!: Record<string, boolean>;
57+
private _cancelledTicks!: Record<string, boolean>;
5858
private _config: StackTraceConfig;
5959
private _disposed?: boolean;
6060
private _fakeTimerAPIs!: TimerAPI;

packages/jest-haste-map/src/ModuleMap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import * as fastPath from './lib/fast_path';
1919
import H from './constants';
2020

21-
const EMPTY_OBJ = {} as {[key: string]: any};
21+
const EMPTY_OBJ = {} as Record<string, any>;
2222
const EMPTY_MAP = new Map();
2323

2424
type ValueType<T> = T extends Map<string, infer V> ? V : never;

packages/jest-jasmine2/src/assertionErrorMessage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import {
1414
import chalk from 'chalk';
1515
import {AssertionErrorWithStack} from './types';
1616

17-
const assertOperatorsMap: {[key: string]: string} = {
17+
const assertOperatorsMap: Record<string, string> = {
1818
'!=': 'notEqual',
1919
'!==': 'notStrictEqual',
2020
'==': 'equal',
2121
'===': 'strictEqual',
2222
};
2323

24-
const humanReadableOperators: {[key: string]: string} = {
24+
const humanReadableOperators: Record<string, string> = {
2525
deepEqual: 'to deeply equal',
2626
deepStrictEqual: 'to deeply and strictly equal',
2727
equal: 'to be equal',

packages/jest-jasmine2/src/jasmine/Env.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default function(j$: Jasmine) {
6666
) => Promise<void>;
6767
fdescribe: (description: string, specDefinitions: Function) => Suite;
6868
spyOn: (
69-
obj: {[key: string]: any},
69+
obj: Record<string, any>,
7070
methodName: string,
7171
accessType?: keyof PropertyDescriptor,
7272
) => Spy;
@@ -94,7 +94,7 @@ export default function(j$: Jasmine) {
9494
const realSetTimeout = global.setTimeout;
9595
const realClearTimeout = global.clearTimeout;
9696

97-
const runnableResources: {[key: string]: {spies: Array<Spy>}} = {};
97+
const runnableResources: Record<string, {spies: Array<Spy>}> = {};
9898
const currentlyExecutingSuites: Array<Suite> = [];
9999
let currentSpec: Spec | null = null;
100100
let throwOnExpectationFailure = false;

packages/jest-jasmine2/src/jasmine/JsApiReporter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default class JsApiReporter implements Reporter {
5353
suiteStarted: (result: SuiteResult) => void;
5454
suiteDone: (result: SuiteResult) => void;
5555
suiteResults: (index: number, length: number) => Array<SuiteResult>;
56-
suites: () => {[key: string]: SuiteResult};
56+
suites: () => Record<string, SuiteResult>;
5757

5858
specResults: (index: number, length: number) => Array<SpecResult>;
5959
specDone: (result: SpecResult) => void;
@@ -95,7 +95,7 @@ export default class JsApiReporter implements Reporter {
9595
};
9696

9797
const suites: Array<SuiteResult> = [];
98-
const suites_hash: {[key: string]: SuiteResult} = {};
98+
const suites_hash: Record<string, SuiteResult> = {};
9999

100100
this.specStarted = function() {};
101101

packages/jest-jasmine2/src/jasmine/createSpy.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ import {Spy} from '../types';
3434
import CallTracker, {Context} from './CallTracker';
3535
import SpyStrategy from './SpyStrategy';
3636

37-
interface Fn {
37+
interface Fn extends Record<string, any> {
3838
(): any;
39-
[key: string]: any;
4039
}
4140

4241
function createSpy(name: string, originalFn: Fn): Spy {

packages/jest-jasmine2/src/jasmine/spyRegistry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ const getErrorMsg = formatErrorMsg('<spyOn>', 'spyOn(<object>, <methodName>)');
5454
export default class SpyRegistry {
5555
allowRespy: (allow: unknown) => void;
5656
spyOn: (
57-
obj: {[key: string]: any},
57+
obj: Record<string, any>,
5858
methodName: string,
5959
accessType?: keyof PropertyDescriptor,
6060
) => Spy;
6161
clearSpies: () => void;
6262
respy: unknown;
6363

6464
private _spyOnProperty: (
65-
obj: {[key: string]: any},
65+
obj: Record<string, any>,
6666
propertyName: string,
6767
accessType: keyof PropertyDescriptor,
6868
) => Spy;

0 commit comments

Comments
 (0)