Reputation: 1345
I'm trying to use the popular react-phone-number-input package, but, I get various errors with typescript, When using the option with country
everything is find, what I did so far:
npm install --save react-phone-number-input
npm install --save @types/react-phone-number-input
import PhoneInput from 'react-phone-number-input'
<PhoneInput/>
But,for without country
we gotta do something like this as pointed out in their site:
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} />
)
}
but, the problem is typescript complains, and it seems they forgot to mention @types thing for `react-phone-number-input/input.
This is the specific error I get:
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';
How do I solve this problem? Any workarounds or installations that are mentioned in the docs?
Upvotes: 4
Views: 6195
Reputation: 56
I believe it's an error in their instructions. Judging by their demo, it should be
import Input from 'react-phone-number-input/input'
<Input
placeholder="Enter phone number"
value={value}
onChange={setValue} />
Note that it's Input, not PhoneInput.
Upvotes: 2
Reputation: 1345
Create custom type declaration.Steps:
Here I present only a minimal bit of type which were as per my requirements sufficient, but it'd be sufficient to build upon:
/// <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> {}
}
Upvotes: 2