Skip to content

Commit

Permalink
feat: Introduce explicit typing for Form.
Browse files Browse the repository at this point in the history
- remove use of 'any' type
- add stricter rule for use of any for linting
  • Loading branch information
programmiri committed Oct 28, 2022
1 parent da3bea4 commit deceda4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
3 changes: 2 additions & 1 deletion coral/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = {
"error"
],
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error"
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-explicit-any": "error"
}
}
11 changes: 7 additions & 4 deletions coral/src/app/components/Form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import {
TextInput,
useForm,
} from "src/app/components/Form";
import { z } from "zod";
import { z, ZodSchema } from "zod";

type WrapperProps<T extends FieldValues> = {
schema: any;
schema: ZodSchema;
defaultValues?: DeepPartial<T>;
onSubmit: SubmitHandler<T>;
onError: SubmitErrorHandler<T>;
Expand Down Expand Up @@ -57,7 +57,10 @@ describe("Form", () => {

const renderForm = <T extends FieldValues>(
children: React.ReactNode,
{ schema, defaultValues }: { schema: any; defaultValues?: DeepPartial<T> }
{
schema,
defaultValues,
}: { schema: ZodSchema; defaultValues?: DeepPartial<T> }
) => {
return render(
<Wrapper<T>
Expand All @@ -82,7 +85,7 @@ describe("Form", () => {
await user.click(screen.getByRole("button", { name: "Submit" }));
};

const assertSubmitted = (data: any) => {
const assertSubmitted = (data: Record<string, string>) => {
expect(onSubmit).toHaveBeenCalledWith(data, expect.anything());
};

Expand Down
9 changes: 2 additions & 7 deletions coral/src/app/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
UseFormProps as _UseFormProps,
UseFormReturn,
} from "react-hook-form";
import { ZodSchema } from "zod";

// ts-jest can't use the import statement for "lodash/get"
// using required makes us able to still use get without
Expand All @@ -46,9 +47,7 @@ type UseFormProps<T extends FieldValues = FieldValues> = Omit<
_UseFormProps<T>,
"resolver"
> & {
//@TODO fix typing
//eslint-disable-next-line @typescript-eslint/no-explicit-any
schema?: any;
schema?: ZodSchema;
};

export const useForm = <T extends FieldValues = FieldValues>({
Expand Down Expand Up @@ -98,9 +97,6 @@ function _TextInput<T extends FieldValues>({
type="text"
{...form.register(name)}
valid={error ? false : undefined}
// @TODO fix typing
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
error={error}
/>
);
Expand Down Expand Up @@ -132,7 +128,6 @@ function _SubmitButton<T extends FieldValues>({

const SubmitButtonMemo = memo(
_SubmitButton,
// @TODO fix typing
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(_prev: FormRegisterProps, _next: FormRegisterProps) => {
return false;
Expand Down

0 comments on commit deceda4

Please sign in to comment.