亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在反應打字稿中獲取輸入字段類型作為道具

如何在反應打字稿中獲取輸入字段類型作為道具

守著星空守著你 2023-07-29 16:06:17
我如何獲取輸入字段類型作為反應打字稿中的道具我嘗試將類型作為字符串發送,但它給出了此錯誤**沒有重載與此調用匹配。重載第 1 個(共 2 個)“(props:InputProps | Readonly):輸入”出現以下錯誤。類型“字符串”不可分配給類型“數字”| “按鈕”| “選擇” | “文本區域”| “時間”| “圖像”| “文本” | “隱藏”| “顏色” | “電子郵件” | “文件” | “廣播”| “復選框”| “重置” | “提交”| “日期” | “日期時間本地”| ... 8 更多... | 不明確的'。重載 2 個,共 2 個,“(props: InputProps, context: any): Input”,出現以下錯誤。類型“字符串”不可分配給類型“數字”| “按鈕”| “選擇”| “文本區域”| “時間”| “圖像”| “文本” | “隱藏”| “顏色” | “電子郵件” | “文件” | “廣播”| “復選框” | “重置” | “提交”| “日期” | “日期時間本地”| ... 8 更多... | 不明確的'。TS2769**這是我的代碼import React from 'react';import { Input } from 'reactstrap';interface IconInputProps {    name: string,    label: string,    placeholder: string,    required: boolean,    errorMessage: string,    autoFocus: boolean,    type: string    icon: string}class IconInput extends React.Component<IconInputProps> {    render() {        return (            <div>                <Input                    name={this.props.name}                    lable={this.props.label}                    type={this.props.type}                    placeholder={this.props.placeholder}                />            </div>        );    }}export default IconInput;
查看完整描述

3 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

您可以將類型顯式聲明為:


import React, { ComponentProps } from 'react';

import { Input } from 'reactstrap';


interface IconInputProps {

    type: ComponentProps<typeof Input>['type'];

    // ...

}

這會傳遞特定組件 prop 的類型聲明,即使給定的庫/組件未導出該類型,它也會起作用。


但有一些注意事項:


不適用于靜態聲明的默認道具和通用道具


來源:https ://github.com/piotrwitek/react-redux-typescript-guide#reactcomponentpropstypeof-xxx


查看完整回答
反對 回復 2023-07-29
?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

您可以嘗試擴展InputProps您應該從中導入的內容@types/reactstrap(我猜它有類型)


在您的界面中,只需添加InputProps. 所以你可以刪除type, name 等等。所以你的代碼看起來像


interface IIconInputProps extends InputProps {

    label: string,

    errorMessage: string,

    icon: string

}

另外我建議名稱以 開頭interface,I這樣你就知道它是一個接口。


查看完整回答
反對 回復 2023-07-29
?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

type: string應替換為type: InputType

并且不要忘記導入這個import { InputType } from "reactstrap/lib/Input.d";


查看完整回答
反對 回復 2023-07-29
  • 3 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號