gpt4 book ai didi

typescript - 如何让 Typescript 获取声明文件?

转载 作者:行者123 更新时间:2023-12-04 00:03:49 24 4
gpt4 key购买 nike

我的文件 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';

我更新了我的 tsconfig.json文件包括:
"typeRoots": [
"./node_modules/@types",
"./src/@types"
]

我的声明文件是这样的:
declare module 'auth0' {
/*...*/
}

为什么没有收到这份声明?

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

最佳答案

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

以上将起作用,但是由于您的声明文件正在声明一个模块,因此最好设置模块解析来查找您的声明文件,而不是使用typeRoots。 ,主要用于全局声明。为此,请使用 baseUrl and paths 或创建 package.json名为 @types/auth0 的本地 npm 包的文件并在您的主 package.json 中注册该包使用相对路径的文件。

关于typescript - 如何让 Typescript 获取声明文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53324112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com