gpt4 book ai didi

typescript - 编译器无法识别自定义 TS 类型

转载 作者:行者123 更新时间:2023-12-02 20:21:40 24 4
gpt4 key购买 nike

我在 src/@types/yargs-interactive/index.d.ts 处有一个为 https://github.com/nanovazquez/yargs-interactive 创建的打字文件。其内容如下:

declare function yargsInteractive(): any;

declare namespace yargsInteractive {
interface OptionData {
type: string;
describe: string;
default?: string | number | boolean;
prompt?: string;
}

interface Option {
[key: string]: OptionData;
}

interface Interactive {
usage(usage: string): any;
interactive(options: Option[]): any;
then(callback: (result: any) => any): any;
}
}

export = yargsInteractive;

但是,当我尝试导入它时,我得到的只是无法找到模块“yargs-interactive”的声明文件错误。我尝试更改 tsconfig.json 文件以将 typeRoots 添加到 src/@types 目录,但仍然找不到它。当它位于 node_modules/@types/yargs-interactive/index.d.ts 下时,它会识别它。

我在这里做错了什么?

最佳答案

根据this issue comment, typeRoots 用于使用全局可用的类型,而不是那些应该导入的类型。尝试使用 path mapping相反:

{
"compilerOptions": {
"baseUrl": ".", // baseUrl is required for paths to work
"paths": {
"*": ["src/@types/*"]
},
// other options...

关于typescript - 编译器无法识别自定义 TS 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51228582/

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