“Formik + Yup + Typescript, 绑定元素 ‘field’ 隐含地具有 ‘any’ 类型。ts”

huangapple go评论72阅读模式
英文:

Formik + Yup + Typescript, Binding element 'field' implicitly has an 'any' type.ts

问题

以下是您的代码,没有进行翻译:

interface Values {
  password: string;
  email: string;
}

let formSchema = object().shape({
  email: string().email().required("Email is required"),
  password: string().required("Password is required"),
});

const Login: React.FC = () => {
  return (
    <Container
      maxW="lg"
      py={{ base: "12", md: "24" }}
      px={{ base: "0", sm: "8" }}
    >
      <Stack spacing="8">
        <Stack spacing="6">
          <Stack spacing={{ base: "2", md: "3" }} textAlign="center">
            <Heading size={useBreakpointValue({ base: "xs", md: "sm" })}>
              Log in to your account
            </Heading>
          </Stack>
        </Stack>
        <Box
          py={{ base: "0", sm: "8" }}
          px={{ base: "4", sm: "10" }}
          bg={useBreakpointValue({ base: "transparent", sm: "bg-surface" })}
          boxShadow={{ base: "none", sm: useColorModeValue("md", "md-dark") }}
          borderRadius={{ base: "none", sm: "xl" }}
        >
          <Formik
            initialValues={{
              password: "",
              email: "",
            }}
            validationSchema={formSchema}
            onSubmit={(values: Values) => {
              console.log("submiting");
              console.log(values);
            }}
          >
            <Form>
              <Stack spacing="6">
                <Stack spacing="5">
                  <Field name="email" id="email" type="email">
                    {({ field, form }) => (
                      <FormControl
                        isInvalid={form.errors.email && form.touched.email}
                      >
                        <FormLabel htmlFor="email">Email</FormLabel>
                        <Input {...field} />
                        <FormErrorMessage>{form.errors.email}</FormErrorMessage>
                      </FormControl>
                    )}
                  </Field>
                  <Field name="password" id="password" type="password">
                    {({ field, form }) => (
                      <FormControl
                        isInvalid={
                          form.errors.password && form.touched.password
                        }
                      >
                        <FormLabel htmlFor="password">Password</FormLabel>
                        <PasswordField {...field} />
                        <FormErrorMessage>
                          {form.errors.password}
                        </FormErrorMessage>
                      </FormControl>
                    )}
                  </Field>
                </Stack>
                <HStack justify="space-between">
                  <Checkbox defaultChecked>Remember me</Checkbox>
                  <Button variant="link" colorScheme="blue" size="sm">
                    Forgot password?
                  </Button>
                </HStack>
                <Stack spacing="6">
                  <Button variant="primary">Sign in</Button>
                </Stack>
              </Stack>
            </Form>
          </Formik>
        </Box>
      </Stack>
    </Container>
  );
};

export default Login;

希望这有助于您理解和修改代码中的问题。如果您需要更多帮助,请随时提出。

英文:

below is my code with a screen shot of the error. The screen shot is for visual aid only. The code snippet that produces the error is right below it.

The error is TypeScript saying that the binding element has implicitly any type. I am not sure what type it is supposed to have. I am very new to TypeScript and I'd appreciate any clarification on the error and pointers to how to fix it.

I'd greatly appreciate any help. I've done some research but the ones I found related to this issue all marked as fixed a while ago yet here I am. It is probably me then “Formik + Yup + Typescript, 绑定元素 ‘field’ 隐含地具有 ‘any’ 类型。ts”

I am using Chakra UI

The code inspired by the Formik example as well as the example on Chakra UI docs

“Formik + Yup + Typescript, 绑定元素 ‘field’ 隐含地具有 ‘any’ 类型。ts”

interface Values {
password: string;
email: string;
}
let formSchema = object().shape({
email: string().email().required(&quot;Email is required&quot;),
password: string().required(&quot;Password is required&quot;),
});
const Login: React.FC = () =&gt; {
return (
&lt;Container
maxW=&quot;lg&quot;
py={{ base: &quot;12&quot;, md: &quot;24&quot; }}
px={{ base: &quot;0&quot;, sm: &quot;8&quot; }}
&gt;
&lt;Stack spacing=&quot;8&quot;&gt;
&lt;Stack spacing=&quot;6&quot;&gt;
&lt;Stack spacing={{ base: &quot;2&quot;, md: &quot;3&quot; }} textAlign=&quot;center&quot;&gt;
&lt;Heading size={useBreakpointValue({ base: &quot;xs&quot;, md: &quot;sm&quot; })}&gt;
Log in to your account
&lt;/Heading&gt;
&lt;/Stack&gt;
&lt;/Stack&gt;
&lt;Box
py={{ base: &quot;0&quot;, sm: &quot;8&quot; }}
px={{ base: &quot;4&quot;, sm: &quot;10&quot; }}
bg={useBreakpointValue({ base: &quot;transparent&quot;, sm: &quot;bg-surface&quot; })}
boxShadow={{ base: &quot;none&quot;, sm: useColorModeValue(&quot;md&quot;, &quot;md-dark&quot;) }}
borderRadius={{ base: &quot;none&quot;, sm: &quot;xl&quot; }}
&gt;
&lt;Formik
initialValues={{
password: &quot;&quot;,
email: &quot;&quot;,
}}
validationSchema={formSchema}
onSubmit={(values: Values) =&gt; {
console.log(&quot;submiting&quot;);
console.log(values);
}}
&gt;
&lt;Form&gt;
&lt;Stack spacing=&quot;6&quot;&gt;
&lt;Stack spacing=&quot;5&quot;&gt;
&lt;Field name=&quot;email&quot; id=&quot;email&quot; type=&quot;email&quot;&gt;
{({ field, form }) =&gt; (
&lt;FormControl
isInvalid={form.errors.email &amp;&amp; form.touched.email}
&gt;
&lt;FormLabel htmlFor=&quot;email&quot;&gt;Email&lt;/FormLabel&gt;
&lt;Input {...field} /&gt;
&lt;FormErrorMessage&gt;{form.errors.email}&lt;/FormErrorMessage&gt;
&lt;/FormControl&gt;
)}
&lt;/Field&gt;
&lt;Field name=&quot;password&quot; id=&quot;password&quot; type=&quot;password&quot;&gt;
{({ field, form }) =&gt; (
&lt;FormControl
isInvalid={
form.errors.password &amp;&amp; form.touched.password
}
&gt;
&lt;FormLabel htmlFor=&quot;password&quot;&gt;Password&lt;/FormLabel&gt;
&lt;PasswordField {...field} /&gt;
&lt;FormErrorMessage&gt;
{form.errors.password}
&lt;/FormErrorMessage&gt;
&lt;/FormControl&gt;
)}
&lt;/Field&gt;
&lt;/Stack&gt;
&lt;HStack justify=&quot;space-between&quot;&gt;
&lt;Checkbox defaultChecked&gt;Remember me&lt;/Checkbox&gt;
&lt;Button variant=&quot;link&quot; colorScheme=&quot;blue&quot; size=&quot;sm&quot;&gt;
Forgot password?
&lt;/Button&gt;
&lt;/HStack&gt;
&lt;Stack spacing=&quot;6&quot;&gt;
&lt;Button variant=&quot;primary&quot;&gt;Sign in&lt;/Button&gt;
&lt;/Stack&gt;
&lt;/Stack&gt;
&lt;/Form&gt;
&lt;/Formik&gt;
&lt;/Box&gt;
&lt;/Stack&gt;
&lt;/Container&gt;
);
};
export default Login;

答案1

得分: 1

你只需要像这样定义你的fieldProp

interface FieldProps {
  field: {
    name: string;
    value: any;
    onChange: (event: React.ChangeEvent<any>) => void;
    onBlur: (event: React.FocusEvent<any>) => void;
  };
  form: {
    touched: { [field: string]: boolean };
    errors: { [field: string]: string };
  };
  meta: {
    error?: string;
    touched?: boolean;
  };
}

然后,你可以像这样将这些fieldform作为props传递:

({ field, form, meta }: FieldProps) => ()
英文:

You just need to define your fieldProp like so

 interface FieldProps {
field: {
name: string;
value: any;
onChange: (event: React.ChangeEvent&lt;any&gt;) =&gt; void;
onBlur: (event: React.FocusEvent&lt;any&gt;) =&gt; void;
};
form: {
touched: { [field: string]: boolean };
errors: { [field: string]: string };
};
meta: {
error?: string;
touched?: boolean;
};
} 

then you are good to go, pass these field and the form as props like so

({field, form, meta}: FieldProps)=&gt;()

答案2

得分: 0

以下是您要求的代码的中文翻译:

import { ErrorMessage, Field, FieldProps, Form, Formik } from "formik";

...

<Field name="termsAndConditions">
  {({ field }: FieldProps) => (
    <div>
      <FormControlLabel
        control={
          <Checkbox
            checked={field.value}
            {...field}
          />
        }
        label="我同意条款和条件"
      />

      <ErrorMessage
        name="termsAndConditions"
        component="div"
      />
    </div>
  )}
</Field>

如果您需要更多翻译或有其他问题,请随时告诉我。

英文:

Example with tsx:

import { ErrorMessage, Field, FieldProps, Form, Formik } from &quot;formik&quot;;

...

&lt;Field name=&quot;termsAndConditions&quot;&gt;
  {({ field }: FieldProps) =&gt; (
    &lt;div&gt;
      &lt;FormControlLabel
        control={
          &lt;Checkbox
            checked={field.value}
            {...field}
          /&gt;
        }
        label=&quot;I agree to the terms and conditions&quot;
      /&gt;

      &lt;ErrorMessage
        name=&quot;termsAndConditions&quot;
        component=&quot;div&quot;
      /&gt;
    &lt;/div&gt;
  )}
&lt;/Field&gt;

huangapple
  • 本文由 发表于 2023年1月6日 10:38:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定