A类型A不适合使用Antdesign的打字稿中的B型分配
大家好,我正在使用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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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
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
Try:
尝试:( antd ver5 ~~)
try: (antd ver5~~)