Skip to content

Commit

Permalink
Improved types.
Browse files Browse the repository at this point in the history
  • Loading branch information
radekmie authored Mar 5, 2021
1 parent 950ba44 commit a182a92
Show file tree
Hide file tree
Showing 47 changed files with 131 additions and 121 deletions.
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"@typescript-eslint/require-await": "off",
"@typescript-eslint/restrict-plus-operands": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/unbound-method": "off",
"import/default": "off",
"jsx-a11y/alt-text": "off",
"jsx-a11y/anchor-is-valid": "off",
Expand Down
6 changes: 3 additions & 3 deletions packages/uniforms-antd/__tests__/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ test('<DateField> - renders an input with correct readOnly state', () => {
);

expect(wrapper.find(DatePicker)).toHaveLength(1);
// @ts-ignore
// @ts-expect-error
expect(wrapper.find(DatePicker).prop('onChange')(now)).toBeFalsy();
expect(onChange).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -119,7 +119,7 @@ test('<DateField> - renders a input which correctly reacts on change', () => {
);

expect(wrapper.find(DatePicker)).toHaveLength(1);
// @ts-ignore
// @ts-expect-error
expect(wrapper.find(DatePicker).prop('onChange')(now)).toBeFalsy();
expect(onChange).toHaveBeenLastCalledWith('x', now.toDate());
});
Expand All @@ -134,7 +134,7 @@ test('<DateField> - renders a input which correctly reacts on change (empty)', (
);

expect(wrapper.find(DatePicker)).toHaveLength(1);
// @ts-ignore
// @ts-expect-error
expect(wrapper.find(DatePicker).prop('onChange')(undefined)).toBeFalsy();
expect(onChange).toHaveBeenLastCalledWith('x', undefined);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/uniforms-antd/__tests__/RadioField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test('<RadioField> - renders a set of checkboxes with correct readOnly state', (

expect(wrapper.find(Radio.Group)).toHaveLength(1);
expect(
// @ts-ignore
// @ts-expect-error
wrapper.find(Radio.Group).prop('onChange')({ target: { value: 'b' } }),
).toBeFalsy();
expect(onChange).not.toHaveBeenCalled();
Expand Down Expand Up @@ -153,7 +153,7 @@ test('<RadioField> - renders a set of checkboxes which correctly reacts on chang

expect(wrapper.find(Radio.Group)).toHaveLength(1);
expect(
// @ts-ignore
// @ts-expect-error
wrapper.find(Radio.Group).prop('onChange')({ target: { value: 'b' } }),
).toBeFalsy();
expect(onChange).toHaveBeenLastCalledWith('x', 'b');
Expand All @@ -173,7 +173,7 @@ test('<RadioField> - renders a set of checkboxes which correctly reacts on chang

expect(wrapper.find(Radio.Group)).toHaveLength(1);
expect(
// @ts-ignore
// @ts-expect-error
wrapper.find(Radio.Group).prop('onChange')({ target: { value: 'a' } }),
).toBeFalsy();
expect(onChange).toHaveBeenLastCalledWith('x', 'a');
Expand Down
20 changes: 10 additions & 10 deletions packages/uniforms-antd/__tests__/SelectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ test('<SelectField> - renders a select with correct name', () => {
});

test('<SelectField> - renders a select with correct options', () => {
// @ts-ignore Is open a valid prop?
// @ts-expect-error Is open a valid prop?
const element = <SelectField name="x" open />;
const wrapper = mount(
element,
Expand All @@ -88,19 +88,19 @@ test('<SelectField> - renders a select with correct options', () => {

expect(wrapper.find(Select)).toHaveLength(1);
expect(wrapper.find(Select).prop('children')).toHaveLength(2);
// @ts-ignore Check children type.
// @ts-expect-error Check children type.
expect(wrapper.find(Select).prop('children')[0].props.value).toBe('a');
// @ts-ignore Check children type.
// @ts-expect-error Check children type.
expect(wrapper.find(Select).prop('children')[0].props.children).toBe('a');
// @ts-ignore Check children type.
// @ts-expect-error Check children type.
expect(wrapper.find(Select).prop('children')[1].props.value).toBe('b');
// @ts-ignore Check children type.
// @ts-expect-error Check children type.
expect(wrapper.find(Select).prop('children')[1].props.children).toBe('b');
});

test('<SelectField> - renders a select with correct options (transform)', () => {
const element = (
// @ts-ignore Is open a valid prop?
// @ts-expect-error Is open a valid prop?
<SelectField name="x" open transform={x => x.toUpperCase()} />
);
const wrapper = mount(
Expand All @@ -110,13 +110,13 @@ test('<SelectField> - renders a select with correct options (transform)', () =>

expect(wrapper.find(Select)).toHaveLength(1);
expect(wrapper.find(Select).prop('children')).toHaveLength(2);
// @ts-ignore Check children type.
// @ts-expect-error Check children type.
expect(wrapper.find(Select).prop('children')[0].props.value).toBe('a');
// @ts-ignore Check children type.
// @ts-expect-error Check children type.
expect(wrapper.find(Select).prop('children')[0].props.children).toBe('A');
// @ts-ignore Check children type.
// @ts-expect-error Check children type.
expect(wrapper.find(Select).prop('children')[1].props.value).toBe('b');
// @ts-ignore Check children type.
// @ts-expect-error Check children type.
expect(wrapper.find(Select).prop('children')[1].props.children).toBe('B');
});

Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-antd/__tests__/wrapField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test('<wrapField> - renders wrapper with label', () => {
const element = wrapField({ label: 'Label' }, <div />);
const wrapper = mount(element);

// @ts-ignore Correct label type.
// @ts-expect-error Correct label type.
expect(wrapper.find(Form.Item).prop('label').props.children[0]).toBe('Label');
});

Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-antd/src/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function Date({
}
}}
placeholder={props.placeholder}
// @ts-ignore: `DatePicker` is an intersection.
// @ts-expect-error: `DatePicker` is an intersection.
ref={props.inputRef}
showTime={showTime}
style={style}
Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-antd/src/RadioField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import wrapField from './wrapField';
export type RadioFieldProps = FieldProps<
string,
RadioProps,
{ allowedValues?: string[]; transform?(value: string): string }
{ allowedValues?: string[]; transform?: (value: string) => string }
>;

const radioStyle = { display: 'block' };
Expand Down
20 changes: 12 additions & 8 deletions packages/uniforms-antd/src/SelectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,43 @@ import { FieldProps, connectField, filterDOMProps } from 'uniforms';
import wrapField from './wrapField';

type CheckboxesProps = FieldProps<
CheckboxValueType,
SelectFieldValue,
CheckboxGroupProps | RadioGroupProps,
{
allowedValues?: CheckboxValueType[];
checkboxes: true;
disableItem?(value: CheckboxValueType): boolean;
disableItem?: (value: CheckboxValueType) => boolean;
inputRef?: Ref<typeof CheckboxGroup | typeof RadioGroup>;
required?: boolean;
transform?(value: CheckboxValueType): string;
transform?: (value: CheckboxValueType) => string;
}
>;

type SelectProps = FieldProps<
string | (string | undefined)[],
SelectFieldValue,
SelectAntDProps<string | string[]>,
{
allowedValues?: string[];
checkboxes?: false;
disableItem?(value: CheckboxValueType): boolean;
disableItem?: (value: CheckboxValueType) => boolean;
inputRef?: Ref<typeof SelectAntD>;
required?: boolean;
transform?(value: string): string;
transform?: (value: string) => string;
}
>;

// This type is needed for the `SelectFieldProps` union to be a proper subtype
// of `Partial<GuaranteedProps<Value>>` - otherwise `connectField` goes wild.
type SelectFieldValue = CheckboxValueType | (string | undefined)[];

export type SelectFieldProps = CheckboxesProps | SelectProps;

function Select(props: SelectFieldProps) {
const Group = props.fieldType === Array ? CheckboxGroup : RadioGroup;
return wrapField(
props,
props.checkboxes ? (
// @ts-ignore: Incorrect `value` type.
// @ts-expect-error: Incorrect `value` type.
<Group
disabled={props.disabled}
name={props.name}
Expand Down Expand Up @@ -77,7 +81,7 @@ function Select(props: SelectFieldProps) {
}
}}
placeholder={props.placeholder}
// @ts-ignore: Incorrect `inputRef` type.
// @ts-expect-error: Incorrect `inputRef` type.
ref={props.inputRef}
value={
props.fieldType === Array
Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-bootstrap3/src/RadioField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type RadioFieldProps = HTMLFieldProps<
allowedValues?: string[];
inline?: boolean;
inputClassName?: string;
transform?(value: string): string;
transform?: (value: string) => string;
}
>;

Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-bootstrap3/src/SelectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type SelectFieldProps = HTMLFieldProps<
inline?: boolean;
inputClassName?: string;
inputRef?: Ref<HTMLSelectElement>;
transform?(value: string): string;
transform?: (value: string) => string;
}
>;

Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-bootstrap4/src/RadioField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type RadioFieldProps = HTMLFieldProps<
allowedValues?: string[];
inline?: boolean;
inputClassName?: string;
transform?(value: string): string;
transform?: (value: string) => string;
}
>;

Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-bootstrap4/src/SelectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type SelectFieldProps = HTMLFieldProps<
inline?: boolean;
inputClassName?: string;
inputRef?: Ref<HTMLSelectElement>;
transform?(value: string): string;
transform?: (value: string) => string;
}
>;

Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms-bootstrap4/src/gridClassName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function gridClassName(
return (
(Object.keys(grid) as GridSize[])
.sort(compareSizeClass)
// @ts-ignore Weird type refinement problem.
// @ts-expect-error Weird type refinement problem.
.map(size => gridClassNamePart(size, grid[size], side))
.join(' ')
);
Expand Down
6 changes: 3 additions & 3 deletions packages/uniforms-bridge-graphql/src/GraphQLBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export default class GraphQLBridge extends Bridge {
super();

// Memoize for performance and referential equality.
this.getField = memoize(this.getField);
this.getSubfields = memoize(this.getSubfields);
this.getType = memoize(this.getType);
this.getField = memoize(this.getField.bind(this));
this.getSubfields = memoize(this.getSubfields.bind(this));
this.getType = memoize(this.getType.bind(this));
}

getError(name: string, error: any) {
Expand Down
6 changes: 3 additions & 3 deletions packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ export default class JSONSchemaBridge extends Bridge {
this.schema = distinctSchema(schema);

// Memoize for performance and referential equality.
this.getField = memoize(this.getField);
this.getSubfields = memoize(this.getSubfields);
this.getType = memoize(this.getType);
this.getField = memoize(this.getField.bind(this));
this.getSubfields = memoize(this.getSubfields.bind(this));
this.getType = memoize(this.getType.bind(this));
}

getError(name: string, error: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export default class SimpleSchema2Bridge extends Bridge {
super();

// Memoize for performance and referential equality.
this.getField = memoize(this.getField);
this.getSubfields = memoize(this.getSubfields);
this.getType = memoize(this.getType);
this.getField = memoize(this.getField.bind(this));
this.getSubfields = memoize(this.getSubfields.bind(this));
this.getType = memoize(this.getType.bind(this));
}

getError(name: string, error: any) {
Expand All @@ -21,7 +21,7 @@ export default class SimpleSchema2Bridge extends Bridge {

getErrorMessage(name: string, error: any) {
const scopedError = this.getError(name, error);
// @ts-ignore: `messageForError` has incorrect typing.
// @ts-expect-error: `messageForError` has incorrect typing.
return !scopedError ? '' : this.schema.messageForError(scopedError);
}

Expand All @@ -30,7 +30,7 @@ export default class SimpleSchema2Bridge extends Bridge {
if (Array.isArray(error.details)) {
// FIXME: Correct type for `error`.
return (error.details as any[]).map(error =>
// @ts-ignore: `messageForError` has incorrect typing.
// @ts-expect-error: `messageForError` has incorrect typing.
this.schema.messageForError(error),
);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ export default class SimpleSchema2Bridge extends Bridge {
}

getSubfields(name?: string) {
// @ts-ignore: Typing for `_makeGeneric` is missing.
// @ts-expect-error: Typing for `_makeGeneric` is missing.
return this.schema.objectKeys(SimpleSchema._makeGeneric(name));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('SimpleSchemaBridge', () => {
// Simulate SimpleSchema.
name = name.replace(/\d+/g, '$');

// @ts-ignore: Dynamic `name`.
// @ts-expect-error: Dynamic `name`.
const field = {
a: { type: Object, label: name },
'a.b': { type: Object, label: name },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export default class SimpleSchemaBridge extends Bridge {
super();

// Memoize for performance and referential equality.
this.getField = memoize(this.getField);
this.getSubfields = memoize(this.getSubfields);
this.getType = memoize(this.getType);
this.getField = memoize(this.getField.bind(this));
this.getSubfields = memoize(this.getSubfields.bind(this));
this.getType = memoize(this.getType.bind(this));
}

getError(name: string, error: any) {
Expand Down
6 changes: 3 additions & 3 deletions packages/uniforms-material/__tests__/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ test('<DateField> - renders a Input which correctly reacts on change', () => {
createContext({ x: { type: Date } }, { onChange }),
);

// @ts-ignore Provide a valid EventTarget.
// @ts-expect-error Provide a valid EventTarget.
wrapper.find(Input).props().onChange!({ target: { valueAsNumber: now } });
expect(onChange).toHaveBeenLastCalledWith('x', now);
});
Expand All @@ -112,7 +112,7 @@ test('<DateField> - renders a Input which correctly reacts on change (empty)', (
);

wrapper.find(Input).props().onChange!({
// @ts-ignore Provide a valid EventTarget.
// @ts-expect-error Provide a valid EventTarget.
target: { valueAsNumber: undefined },
});
expect(onChange).toHaveBeenLastCalledWith('x', undefined);
Expand All @@ -128,7 +128,7 @@ test('<DateField> - renders a Input which correctly reacts on change (overflow)'
createContext({ x: { type: Date } }, { onChange }),
);

// @ts-ignore Provide a valid EventTarget.
// @ts-expect-error Provide a valid EventTarget.
wrapper.find(Input).props().onChange!({ target: { valueAsNumber: now } });
expect(onChange).not.toHaveBeenCalled();
});
Expand Down
6 changes: 3 additions & 3 deletions packages/uniforms-material/__tests__/LongTextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ test('<LongTextField> - renders a TextField which correctly reacts on change', (
);

expect(wrapper.find(TextField)).toHaveLength(1);
// @ts-ignore Provide a valid EventTarget.
// @ts-expect-error Provide a valid EventTarget.
wrapper.find(TextField).props().onChange!({ target: { value: 'y' } });
expect(onChange).toHaveBeenLastCalledWith('x', 'y');
});
Expand All @@ -112,7 +112,7 @@ test('<LongTextField> - renders a TextField which correctly reacts on change (em
);

expect(wrapper.find(TextField)).toHaveLength(1);
// @ts-ignore Provide a valid EventTarget.
// @ts-expect-error Provide a valid EventTarget.
wrapper.find(TextField).props().onChange!({ target: { value: '' } });
expect(onChange).toHaveBeenLastCalledWith('x', '');
});
Expand All @@ -127,7 +127,7 @@ test('<LongTextField> - renders a TextField which correctly reacts on change (sa
);

expect(wrapper.find(TextField)).toHaveLength(1);
// @ts-ignore Provide a valid EventTarget.
// @ts-expect-error Provide a valid EventTarget.
wrapper.find(TextField).props().onChange!({ target: { value: 'y' } });
expect(onChange).toHaveBeenLastCalledWith('x', 'y');
});
Expand Down
Loading

0 comments on commit a182a92

Please sign in to comment.