reactjs - 没有国家选择器的 react-phone-number-input
问题描述
我正在尝试使用流行的react-phone-number-input包,但是,打字稿出现各种错误,使用选项时with country
一切都找到了,到目前为止我做了什么:
npm install --save react-phone-number-input
npm install --save @types/react-phone-number-input
import PhoneInput from 'react-phone-number-input'
<PhoneInput/>
但是,因为without country
我们必须按照他们的网站中指出的那样做这样的事情:
import PhoneInput from 'react-phone-number-input/input'
function Example() {
// `value` will be the parsed phone number in E.164 format.
// Example: "+12133734253".
const [value, setValue] = useState()
// If `country` property is not passed
// then "International" format is used.
// Otherwise, "National" format is used.
return (
<PhoneInput
country="US"
value={value}
onChange={setValue} />
)
}
但是,问题是打字稿抱怨,而且他们似乎忘记为 `react-phone-number-input/input 提及 @types 东西。
这是我得到的具体错误:
TS7016: Could not find a declaration file for module 'react-phone-number-input/input'.
'/home/liz/prj/node_modules/react-phone-number-input/input/index.commonjs.js' implicitly has an 'any' type.
Try `npm install @types/react-phone-number-input-input-min` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-phone-number-input/input';
我该如何解决这个问题?文档中提到的任何解决方法或安装?
解决方案
创建自定义类型声明。步骤:
- 创建名为“react-phone-number-input”的文件夹
- 创建子文件夹输入
- 在其中创建 index.d.ts
- 复制并粘贴以下代码
在这里,我只展示了根据我的要求足够的最小类型,但它足以构建:
/// <reference types="react">
declare module 'react-phone-number-input/input' {
interface CustomPhoneInputProps {
onChange: (value: string) => void;
value: string;
placeholder: string;
className: string;
}
export default class PhoneInput extends React.Component<CustomPhoneInputProps, object> {}
}
推荐阅读
- c# - Dllimport/PInvoke 二进制兼容性
- java - VScode 终端输出
- android - Firebase Messaging 将 Android 设备屏幕变黑
- javascript - jQuery nth-child 相对于前一个选择器
- docker - Kubernetes (Docker for Mac) 上的 Apache Spark 无法解析我的 VPN 主机
- powershell - 在 Win32_Service StartName 报告中重复服务器名称
- jquery - 使用 jquery attr 设置 css 这个 attr 值?
- java - 在 for 循环内部或外部添加条件以获得更清晰的代码更好吗?
- html - 如何覆盖标签 - 引导 css 脚手架.less
- java - JavaFX 的媒体播放器不检索元数据