Skip to content

Commit 6effcbb

Browse files
authored
[code-infra] Convert @mui/utils to typescript (#45671)
1 parent ebb24ce commit 6effcbb

24 files changed

+224
-130
lines changed

packages-internal/test-utils/src/chai.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ declare global {
100100
* Asserts that the given callback throws an error matching the given message in development (process.env.NODE_ENV !== 'production').
101101
* In production it expects a minified error.
102102
*/
103-
toThrowMinified(message: string): void;
103+
toThrowMinified(message: string | RegExp): void;
104104
}
105105
}
106106
}

packages/mui-utils/src/capitalize/capitalize.test.js renamed to packages/mui-utils/src/capitalize/capitalize.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe('capitalize', () => {
88

99
it('should throw when not used correctly', () => {
1010
expect(() => {
11+
// @ts-expect-error Testing improper usage
1112
capitalize();
1213
}).toThrowMinified(/expects a string argument/);
1314
});

packages/mui-utils/src/debounce/debounce.test.js renamed to packages/mui-utils/src/debounce/debounce.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { spy, useFakeTimers } from 'sinon';
33
import debounce from './debounce';
44

55
describe('debounce', () => {
6-
let clock;
6+
let clock: ReturnType<typeof useFakeTimers>;
77

88
beforeEach(() => {
99
clock = useFakeTimers();
@@ -16,8 +16,8 @@ describe('debounce', () => {
1616
it('should debounce', () => {
1717
const handler = spy();
1818
const expectedContext = { foo: 'bar' };
19-
let actualContext;
20-
function collectContext(...args) {
19+
let actualContext: any;
20+
function collectContext(this: any, ...args: any[]) {
2121
// eslint-disable-next-line consistent-this
2222
actualContext = this;
2323
handler(...args);

packages/mui-utils/src/deprecatedPropType/deprecatedPropType.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/mui-utils/src/deprecatedPropType/deprecatedPropType.test.js renamed to packages/mui-utils/src/deprecatedPropType/deprecatedPropType.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('deprecatedPropType', () => {
1212

1313
it('should not warn', () => {
1414
const propName = `children${new Date()}`;
15-
const props = {};
15+
const props: Record<string, any> = {};
1616
PropTypes.checkPropTypes(
1717
{
1818
[propName]: deprecatedPropType(PropTypes.string, 'give me a reason'),
@@ -26,7 +26,7 @@ describe('deprecatedPropType', () => {
2626

2727
it('should warn once', () => {
2828
const propName = `children`;
29-
const props = {
29+
const props: Record<string, any> = {
3030
[propName]: 'yolo',
3131
};
3232

packages/mui-utils/src/deprecatedPropType/deprecatedPropType.js renamed to packages/mui-utils/src/deprecatedPropType/deprecatedPropType.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
export default function deprecatedPropType(validator, reason) {
1+
import { Validator } from 'prop-types';
2+
3+
export default function deprecatedPropType<T>(
4+
validator: Validator<T>,
5+
reason: string,
6+
): Validator<T> {
27
if (process.env.NODE_ENV === 'production') {
38
return () => null;
49
}
510

6-
return (props, propName, componentName, location, propFullName) => {
11+
return (
12+
props: Record<string, any>,
13+
propName: string,
14+
componentName?: string,
15+
location?: string,
16+
propFullName?: string,
17+
) => {
718
const componentNameSafe = componentName || '<<anonymous>>';
819
const propFullNameSafe = propFullName || propName;
920

packages/mui-utils/src/integerPropType/integerPropType.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/mui-utils/src/integerPropType/integerPropType.test.js renamed to packages/mui-utils/src/integerPropType/integerPropType.test.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('integerPropType', () => {
99
const location = '';
1010
const componentName = 'DummyComponent';
1111

12-
function checkPropType(props, propName, required) {
12+
function checkPropType(props: Record<string, any>, propName: string, required: boolean): void {
1313
PropTypes.checkPropTypes(
1414
{
1515
[propName]: required ? integerPropType.isRequired : integerPropType,
@@ -20,13 +20,21 @@ describe('integerPropType', () => {
2020
);
2121
}
2222

23-
function assertPass({ props }, propName, required = false) {
23+
function assertPass(
24+
{ props }: { props: Record<string, any> },
25+
propName: string,
26+
required = false,
27+
): void {
2428
expect(() => {
2529
checkPropType(props, propName, required);
2630
}).not.toErrorDev();
2731
}
2832

29-
function assertFail({ props }, propName, required = false) {
33+
function assertFail(
34+
{ props }: { props: Record<string, any> },
35+
propName: string,
36+
required = false,
37+
): void {
3038
const propType = getTypeByValue(props[propName]);
3139
const errorMessage = `Warning: Failed type: Invalid ${location} \`${propName}\` of type \`${propType}\` supplied to \`${componentName}\`, expected \`integer\`.`;
3240

@@ -41,36 +49,44 @@ describe('integerPropType', () => {
4149
});
4250

4351
it('passes on undefined', () => {
52+
// @ts-expect-error div doesn't have an a prop
4453
assertPass(<div a={undefined} />, 'a');
4554
});
4655

4756
it('fails on null', () => {
57+
// @ts-expect-error div doesn't have an a prop
4858
assertFail(<div a={null} />, 'a');
4959
});
5060
});
5161

62+
// @ts-expect-error div doesn't have an a prop
5263
it('passes on zero', () => assertPass(<div a={0} />, 'a'));
5364

5465
it('passes on positive numbers', () => {
66+
// @ts-expect-error div doesn't have an a prop
5567
assertPass(<div a={42} />, 'a');
5668
});
5769

5870
describe('passes with the conversion before passing', () => {
5971
it('passes with conversion - parseInt', () => {
60-
assertPass(<div a={parseInt(1.1, 10)} />, 'a');
72+
// @ts-expect-error div doesn't have an a prop
73+
assertPass(<div a={parseInt('1.1', 10)} />, 'a');
6174
});
6275

6376
it('passes with the conversion - Math.floor', () => {
77+
// @ts-expect-error div doesn't have an a prop
6478
assertPass(<div a={Math.floor(1.1)} />, 'a');
6579
});
6680

6781
it('passes with the boolean conversion', () => {
82+
// @ts-expect-error div doesn't have an a prop
6883
// eslint-disable-next-line no-bitwise
6984
assertPass(<div a={1.1 | 0} />, 'a');
7085
});
7186
});
7287

7388
it('passes on negative numbers', () => {
89+
// @ts-expect-error div doesn't have an a prop
7490
assertPass(<div a={-42} />, 'a');
7591
});
7692

@@ -80,32 +96,39 @@ describe('integerPropType', () => {
8096
});
8197

8298
it('fails when we pass float number', () => {
99+
// @ts-expect-error div doesn't have an a prop
83100
assertFail(<div a={1.5} />, 'a');
84101
});
85102

86103
it('fails when have been made computation which results in float number', () => {
104+
// @ts-expect-error div doesn't have an a prop
87105
assertFail(<div a={(0.1 + 0.2) * 10} />, 'a');
88106
});
89107

90108
it('fails on string', () => {
109+
// @ts-expect-error div doesn't have an a prop
91110
assertFail(<div a={'a message'} />, 'a');
92111
});
93112

94113
it('fails on boolean', () => {
114+
// @ts-expect-error div doesn't have an a prop
95115
assertFail(<div a={false} />, 'a');
96116
});
97117

98118
it('fails on array', () => {
119+
// @ts-expect-error div doesn't have an a prop
99120
assertFail(<div a={[]} />, 'a');
100121
});
101122
});
102123

103124
describe('fails on number edge cases', () => {
104125
it('fails on infinity', () => {
126+
// @ts-expect-error div doesn't have an a prop
105127
assertFail(<div a={Infinity} />, 'a');
106128
});
107129

108130
it('fails on NaN', () => {
131+
// @ts-expect-error div doesn't have an a prop
109132
assertFail(<div a={NaN} />, 'a');
110133
});
111134
});

packages/mui-utils/src/integerPropType/integerPropType.js renamed to packages/mui-utils/src/integerPropType/integerPropType.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export function getTypeByValue(value) {
1+
import PropTypes from 'prop-types';
2+
3+
export function getTypeByValue(value: any): string {
24
const valueType = typeof value;
35
switch (valueType) {
46
case 'number':
@@ -24,7 +26,12 @@ export function getTypeByValue(value) {
2426
}
2527
}
2628

27-
function requiredInteger(props, propName, componentName, location) {
29+
function requiredInteger(
30+
props: Record<string, any>,
31+
propName: string,
32+
componentName: string,
33+
location: string,
34+
): Error | null {
2835
const propValue = props[propName];
2936

3037
if (propValue == null || !Number.isInteger(propValue)) {
@@ -38,21 +45,29 @@ function requiredInteger(props, propName, componentName, location) {
3845
return null;
3946
}
4047

41-
function validator(props, propName, ...other) {
48+
function validator(
49+
props: Record<string, any>,
50+
propName: string,
51+
componentName: string,
52+
location: string,
53+
): Error | null {
4254
const propValue = props[propName];
4355

4456
if (propValue === undefined) {
4557
return null;
4658
}
4759

48-
return requiredInteger(props, propName, ...other);
60+
return requiredInteger(props, propName, componentName, location);
4961
}
5062

51-
function validatorNoop() {
63+
function validatorNoop(): null {
5264
return null;
5365
}
5466

5567
validator.isRequired = requiredInteger;
5668
validatorNoop.isRequired = validatorNoop;
5769

58-
export default process.env.NODE_ENV === 'production' ? validatorNoop : validator;
70+
const integerPropType: PropTypes.Requireable<number> =
71+
process.env.NODE_ENV === 'production' ? validatorNoop : validator;
72+
73+
export default integerPropType;

packages/mui-utils/src/requirePropFactory/requirePropFactory.d.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/mui-utils/src/requirePropFactory/requirePropFactory.test.js renamed to packages/mui-utils/src/requirePropFactory/requirePropFactory.test.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import requirePropFactory from './requirePropFactory';
44

55
describe('requirePropFactory', () => {
66
const componentNameInError = 'componentNameInError';
7-
let requireProp;
7+
let requireProp: (prop: string) => PropTypes.Validator<any>;
88

99
before(() => {
1010
requireProp = requirePropFactory(componentNameInError);
@@ -18,7 +18,7 @@ describe('requirePropFactory', () => {
1818
describe('requireProp()', () => {
1919
const requiredPropName = 'requiredPropName';
2020

21-
let requirePropValidator;
21+
let requirePropValidator: PropTypes.Validator<any>;
2222

2323
before(() => {
2424
requirePropValidator = requireProp(requiredPropName);
@@ -29,8 +29,8 @@ describe('requirePropFactory', () => {
2929
});
3030

3131
describe('requirePropValidator', () => {
32-
let props;
33-
let propName;
32+
let props: Record<string, unknown>;
33+
let propName: string;
3434

3535
beforeEach(() => {
3636
PropTypes.resetWarningCache();
@@ -71,13 +71,14 @@ describe('requirePropFactory', () => {
7171
});
7272

7373
describe('propName is in props and requiredProp not in props', () => {
74-
let result;
74+
let result: Error | null;
7575

7676
before(() => {
7777
props = {};
7878
propName = 'propName';
7979
props[propName] = true;
8080
delete props[requiredPropName];
81+
// @ts-expect-error The validator should be called with the right arguments
8182
result = requirePropValidator(props, propName, undefined, undefined, undefined);
8283
});
8384

@@ -97,19 +98,20 @@ describe('requirePropFactory', () => {
9798
});
9899

99100
describe('propFullName given to validator', () => {
100-
let propFullName;
101+
let propFullName: string;
101102

102103
before(() => {
103104
propFullName = 'propFullName';
105+
// @ts-expect-error The validator should be called with the right arguments
104106
result = requirePropValidator(props, propName, undefined, undefined, propFullName);
105107
});
106108

107109
it('returned error message should have propFullName', () => {
108-
expect(result.message.includes(propFullName)).to.equal(true);
110+
expect(result!.message.includes(propFullName)).to.equal(true);
109111
});
110112

111113
it('returned error message should not have propName', () => {
112-
expect(result.message.includes(propName)).to.equal(false);
114+
expect(result!.message.includes(propName)).to.equal(false);
113115
});
114116
});
115117
});
@@ -122,7 +124,7 @@ describe('requirePropFactory', () => {
122124
test: PropTypes.string,
123125
};
124126

125-
const localProps = {};
127+
const localProps: Record<string, unknown> = {};
126128
const localPropName = 'test';
127129
localProps[localPropName] = 'string';
128130

@@ -150,7 +152,7 @@ describe('requirePropFactory', () => {
150152
test: PropTypes.string,
151153
};
152154

153-
const localProps = {};
155+
const localProps: Record<string, unknown> = {};
154156
const localPropName = 'test';
155157
localProps[localPropName] = true;
156158

packages/mui-utils/src/requirePropFactory/requirePropFactory.js renamed to packages/mui-utils/src/requirePropFactory/requirePropFactory.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
export default function requirePropFactory(componentNameInError, Component) {
1+
import * as React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
export default function requirePropFactory(
5+
componentNameInError: string,
6+
Component?: React.ComponentType<unknown>,
7+
): (requiredProp: string) => PropTypes.Validator<any> {
28
if (process.env.NODE_ENV === 'production') {
3-
return () => null;
9+
return () => () => null;
410
}
511

612
// eslint-disable-next-line react/forbid-foreign-prop-types
713
const prevPropTypes = Component ? { ...Component.propTypes } : null;
814

915
const requireProp =
10-
(requiredProp) =>
16+
(requiredProp: string): PropTypes.Validator<any> =>
1117
(props, propName, componentName, location, propFullName, ...args) => {
1218
const propFullNameSafe = propFullName || propName;
1319

packages/mui-utils/src/unsupportedProp/unsupportedProp.d.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)