如何让 Typescript 获取声明文件?

Dan*_*umb 4 typescript ts-node

我的文件src/auth/ManagementAPI.ts使用 Auth0。我需要使用我自己的声明文件并创建了src/@types/auth0.d.ts.

但是,当我运行时ts-node,出现此错误:

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';
Run Code Online (Sandbox Code Playgroud)

我已经更新了我的tsconfig.json文件以包括:

"typeRoots": [
    "./node_modules/@types",
    "./src/@types"
]
Run Code Online (Sandbox Code Playgroud)

我的声明文件是这样的:

declare module 'auth0' {
  /*...*/
}
Run Code Online (Sandbox Code Playgroud)

为什么这个声明没有被采纳?

我在这里创建了一个示例存储库:https : //bitbucket.org/ohanapediatrics/so-demo/src/master/

Mat*_*hen 9

对于typeRoots加载声明文件的机制,它需要被命名index.d.ts并放置在选项中列出的目录之一的子目录typeRoots;请参阅文档。根据typeRoots存储库中的设置,您可以将文件放在src/types/auth0/index.d.ts.

上面的方法可行,但由于您的声明文件正在声明一个模块,因此最好设置模块解析来查找您的声明文件,而不是使用typeRoots,后者主要用于全局声明。为此,请使用baseUrlpathspackage.json为本地 npm 包创建一个文件,@types/auth0package.json使用相对路径在主文件中注册该包。