Create a declaration file .d.ts file for a npm module does not have @type package
I am trying to use react-geocode with TypeScript but it does not have @type package.
I did following to make it work:
- eject from react-react-app (this somehow made the path mapping work better and you got to add config to tsconfig.ts)
- create react-geocode.d.ts file in src/typings (or any folder of your choosing)
- In tsconfig.js:
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"types": ["types"],
"store": ["store"],
"*": [
"node_modules/*",
"src/typings/*" // this will make the typescript compiler find the type file
]
}
},
"include": [
"src"
]
}
Finally, put something like this in react-geocode.d.ts:
declare module 'react-geocode' {
export function fromAddress(p: any): Promise<any>;
export function fromLatLng(p: any, b: any): Promise<any>;
export function setApiKey(p: any): void;
};
Now the app compiles.