Create a declaration file .d.ts file for a npm module does not have @type package

Steve Mu
1 min readApr 23, 2020

--

I am trying to use react-geocode with TypeScript but it does not have @type package.

I did following to make it work:

  1. eject from react-react-app (this somehow made the path mapping work better and you got to add config to tsconfig.ts)
  2. create react-geocode.d.ts file in src/typings (or any folder of your choosing)
  3. 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.

--

--