6

My file src/auth/ManagementAPI.ts uses Auth0. I need to use my own declaration file and have created src/@types/auth0.d.ts.

However, when I run ts-node, I'm getting this error:

TSError: ⨯ Unable to compile TypeScript:
auth/ManagementAPI.ts(1,69): error TS7016: Could not find a declaration file for module 'auth0'. '/Users/danrumney/WebstormProjects/ohana/node_modules/auth0/src/index.js' implicitly has an 'any' type.
  Try `npm install @types/auth0` if it exists or add a new declaration (.d.ts) file containing `declare module 'auth0';

I've updated my tsconfig.json file to include:

"typeRoots": [
    "./node_modules/@types",
    "./src/@types"
]

My declaration file is like this:

declare module 'auth0' {
  /*...*/
}

Why is this declaration not being picked up?

I've created an example repository here: https://bitbucket.org/ohanapediatrics/so-demo/src/master/

Dancrumb
  • 26,597
  • 10
  • 74
  • 130
  • Do you have an example repo or can you spin up an example on stackblitz? – mwilson Nov 15 '18 at 16:54
  • Does your auth0 declaration file name is `auth0.d.ts` within your `./src/@types` directory ? And the good practice is to put your third-party libraries local declaration files within a `./types` directory. You don't even have to add it in the config, it's automatically parsed. – Ivan Gabriele Nov 15 '18 at 17:05
  • UPDATE: The error is when I use `ts-node`, not `tsc` – Dancrumb Nov 15 '18 at 17:42
  • Added git repo: https://bitbucket.org/ohanapediatrics/so-demo/src/master/ – Dancrumb Nov 15 '18 at 18:01

1 Answers1

11

For the typeRoots mechanism to load your declaration file, it needs to be named index.d.ts and placed in a subdirectory of one of the directories listed in the typeRoots option; see the documentation. Based on the typeRoots setting in your repository, you could put the file at src/types/auth0/index.d.ts.

The above will work, but since your declaration file is declaring a module, it would be better practice to set up module resolution to find your declaration file instead of using typeRoots, which is mainly intended for global declarations. To do that, either use baseUrl and paths or create a package.json file for a local npm package named @types/auth0 and register the package in your main package.json file using a relative path.

Matt McCutchen
  • 28,856
  • 2
  • 68
  • 75
  • 1
    Thanks! From the docs: > A types package is a folder with a file called index.d.ts or a folder with a package.json that has a types field. – Dancrumb Nov 15 '18 at 20:35