A类型A不适合使用Antdesign的打字稿中的B型分配

发布于 2025-02-06 07:56:23 字数 2445 浏览 3 评论 0原文

大家好,我正在使用Ant Design在打字稿中编写此代码,我得到了这个问题(TS2322)。 我想使用“蚂蚁设计”中的“输入”,并将第一个值设置为null。之后,我要将其放入输入中会出现一些错误。有人知道如何解决这个问题吗?感谢您提前的帮助! *仅供参考:我的ANTD版本是4.21.0,打字稿为4.7.2

import { Button, Col, Input, Row } from "antd";
import { useRef } from 'react';
import styles from './Signin.module.css';

export default function Signin() {
const emailRef = useRef<typeof Input>(null);
const passwordRef = useRef<typeof Input>(null);

  return <Row align="middle" className={styles.signin_row}>
    <Col span={24}>
      <Row className={styles.signin_contents}>
        <Col span={12}>
          <img src="/bg_signin.png" alt="Signin" className={styles.signin_bg}/>
        </Col>
        <Col span={12}>
          <div className={styles.signin_title}>My Books</div>
          <div className={styles.signin_subtitle}>Please Note Your Opinion</div>
          <div className={styles.signin_underline} />
          <div className={styles.email_title}>
            Email
            <span className={styles.required}> *</span>
          </div>
          <div className={styles.input_area}>
            <Input 
            placeholder="Email"
            autoComplete="email"
            name="email" className={styles.input}
            ref ={emailRef}/>
          </div>
          <div className={styles.password_title}>
            Password
            <span className={styles.required}> *</span>
          </div>
          <div className={styles.input_area}>
            <Input 
            type="password"
            autoComplete="current-password"
            name="password"
            className={styles.input}
            ref={passwordRef}/>
          </div>
          <div className={styles.button_area}>
            <Button size="large" className={styles.button}>Sign In</Button>
          </div>
        </Col>
      </Row>
    </Col>
  </Row>

}

添加:糟糕,我忘了输入错误消息所说的内容!

”在此处输入图像说明”

Hi guys I was writing this code in typescript with using ant design and I got this issue (TS2322).
I wanted to use 'Input' from 'Ant Design' and set the first value as null. After that I was gonna put it in the Input it occurs some errors. Does anyone know how to solve this issue? Thank you for your help in advance!
*FYI: my antd version is 4.21.0 and typescript is 4.7.2

import { Button, Col, Input, Row } from "antd";
import { useRef } from 'react';
import styles from './Signin.module.css';

export default function Signin() {
const emailRef = useRef<typeof Input>(null);
const passwordRef = useRef<typeof Input>(null);

  return <Row align="middle" className={styles.signin_row}>
    <Col span={24}>
      <Row className={styles.signin_contents}>
        <Col span={12}>
          <img src="/bg_signin.png" alt="Signin" className={styles.signin_bg}/>
        </Col>
        <Col span={12}>
          <div className={styles.signin_title}>My Books</div>
          <div className={styles.signin_subtitle}>Please Note Your Opinion</div>
          <div className={styles.signin_underline} />
          <div className={styles.email_title}>
            Email
            <span className={styles.required}> *</span>
          </div>
          <div className={styles.input_area}>
            <Input 
            placeholder="Email"
            autoComplete="email"
            name="email" className={styles.input}
            ref ={emailRef}/>
          </div>
          <div className={styles.password_title}>
            Password
            <span className={styles.required}> *</span>
          </div>
          <div className={styles.input_area}>
            <Input 
            type="password"
            autoComplete="current-password"
            name="password"
            className={styles.input}
            ref={passwordRef}/>
          </div>
          <div className={styles.button_area}>
            <Button size="large" className={styles.button}>Sign In</Button>
          </div>
        </Col>
      </Row>
    </Col>
  </Row>

}

Add: oops I forgot to put what the error message says!

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很糊涂小朋友 2025-02-13 07:56:23

Typescript消息说明了为什么您的EmailRef类型不匹配输入参考类型类型:

类型'RefoBject&lt; compoundedComponent&gt;'不能分配给'ref&lt; inputref&gt; |未定义的

因此,我的线索是您应该将inputref用作emailref的通用类型:

const emailRef = useref&lt; inputref&gt;();

您可以在'antd docs中找到相似的示例。选项'代码示例:
https://ant.design/components/components/components/tput/#input.tput.textarea

import type { InputRef } from 'antd';

import { Button, Input, Space, Switch } from 'antd';
import React, { useRef, useState } from 'react';

const App: React.FC = () => {
  const inputRef = useRef<InputRef>(null);
  const [input, setInput] = useState(true);
....


const emailRef = useRef<InputRef | null>(null);

Typescript message explains why your emailRef type doesn't match Input ref required type:

Type 'RefObject<CompoundedComponent>' is not assignable to type 'Ref<InputRef> | undefined

So my clue is that you should use InputRef as generic type for emailRef:

const emailRef = useRef<InputRef>();

You can find similar example in antd docs in 'Focus with additional option' code example:
https://ant.design/components/input/#Input.TextArea

import type { InputRef } from 'antd';

import { Button, Input, Space, Switch } from 'antd';
import React, { useRef, useState } from 'react';

const App: React.FC = () => {
  const inputRef = useRef<InputRef>(null);
  const [input, setInput] = useState(true);
....

Try:


const emailRef = useRef<InputRef | null>(null);
狼亦尘 2025-02-13 07:56:23

尝试:( antd ver5 ~~)


const emailRef = useRef<InputRef | null>(null);

try: (antd ver5~~)


const emailRef = useRef<InputRef | null>(null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文