简体   繁体   English

Typedoc / Typescript 编译器在导入的类型上抛出错误

[英]Typedoc / Typescript Compiler throws error on an imported type

Summary概括

I am attempting to build documentation for a TypeScript project using Typedoc.我正在尝试使用 Typedoc 为 TypeScript 项目构建文档。 Unfortunately, Typedoc is yielding errors on an import statement I use in one of my files.不幸的是,Typedoc 在我在其中一个文件中使用的导入语句中产生了错误。

I am confused because the file it is error-checking is not used in my project, or necessarily required to construct my import type.我很困惑,因为我的项目中没有使用它进行错误检查的文件,或者构造我的导入类型所必需的。

Problem Preface问题前言

I am importing a type, using the syntax import type { Type } .我正在导入一个类型,使用语法import type { Type } To be specific, this file export s an interface , and I use a relative path (outside of my project root) in my import statement to access it.具体来说,这个文件export是一个interface ,我在 import 语句中使用相对路径(在我的项目根目录之外)来访问它。 This file is a model generated using openapi-generator.该文件是使用 openapi-generator 生成的模型。 This file also has a bunch of import statements at the top, one of which is called runtime.ts .这个文件的顶部还有一堆import语句,其中一个叫做runtime.ts

Typedoc and my TSConfig appear to be resolving all of these imports and then error-checking them. Typedoc 和我的 TSConfig 似乎正在解析所有这些导入,然后对它们进行错误检查。 Errors from runtime.ts are what appear in my console when I attempt to run Typedoc.当我尝试运行 Typedoc 时,来自runtime.ts的错误出现在我的控制台中。 That is confusing behavior;这是令人困惑的行为; I would have expected my import statements to only include the literal type I am importing, and the enums required for the properties.我本来希望我的导入语句包括我正在导入的文字类型,以及属性所需的枚举。

What I've tried我试过的

I have tried omitting the type keyword (so that the import statement reads import { Type } ), to no avail.我尝试省略 type 关键字(以便 import 语句读取import { Type } ),但无济于事。 The interface is barebones: One string property, two enums.该接口是准系统:一个字符串属性,两个枚举。 I am debating just copying the interface and enum definitions into my project file, but that is a temporary fix and isn't a scaleable solution.我正在辩论只是将接口和枚举定义复制到我的项目文件中,但这是一个临时修复,不是可扩展的解决方案。

Context语境

My tsconfig.json...我的 tsconfig.json...

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */

    /* Projects */

    /* Language and Environment */
    "target": "ES6",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */

    /* Modules */
    "module": "ES6",                                /* Specify what module code is generated. */
    "moduleResolution": "node",                       /* Specify how TypeScript looks up a file from a given module specifier. */



    /* Emit */
    "outDir": "./dist",                                   /* Specify an output folder for all emitted files. */
    "inlineSourceMap": true,                          /* Include sourcemap files inside the emitted JavaScript. */
    "inlineSources": true,                            /* Include source code in the sourcemaps inside the emitted JavaScript. */
    "noEmitOnError": false,                            /* Disable emitting files if any type checking errors are reported. */


    /* Interop Constraints */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
    // "preserveSymlinks": true,                         /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */

    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "noImplicitAny": true,                            /* Enable error reporting for expressions and declarations with an implied `any` type.. */

    /* Completeness */
    "skipDefaultLibCheck": true,                      /* Skip type checking .d.ts files that are included with TypeScript. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  },
  "include": ["./src/content.ts", "./src/content/*.ts"],

}

My typedoc.json...我的 typedoc.json ......

 {
  "entryPoints": ["./src/content.ts"],
  "out": "./doc",
  "tsconfig": "./tsconfig.json",
}

Any help or guidance is much appreciated!非常感谢任何帮助或指导! Thank you for reading.感谢您的阅读。

I would have expected my import statements to only include the literal type I am importing, and the enums required for the properties.我本来希望我的导入语句只包括我正在导入的文字类型,以及属性所需的枚举。

This is an unfortunately incredibly common misunderstanding.不幸的是,这是一个非常普遍的误解。 This isn't how the compiler API works, and therefore isn't how TypeDoc works.这不是编译器 API 的工作方式,因此也不是 TypeDoc 的工作方式。 If you compile your project with npx tsc -p tsconfig.json , you will receive the same errors.如果您使用npx tsc -p tsconfig.json编译项目,您将收到相同的错误。

runtime.ts:93:21 - error TS2322: Type '(url: string, init: RequestInit) => Promise' is not assignable to type '{ (input: RequestInfo, init?: RequestInit | undefined): Promise; runtime.ts:93:21 - 错误 TS2322:类型 '(url: string, init: RequestInit) => Promise' 不可分配给类型 '{ (input: RequestInfo, init?: RequestInit | undefined): Promise; (input: RequestInfo, init?: RequestInit | undefined): Promise<...>; (输入:RequestInfo, init?: RequestInit | undefined): Promise<...>; }'. }'。 Types of parameters 'url' and 'input' are incompatible.参数 'url' 和 'input' 的类型不兼容。 Type 'RequestInfo' is not assignable to type 'string'.类型“RequestInfo”不可分配给类型“字符串”。 Type 'Request' is not assignable to type 'string'.类型“请求”不可分配给类型“字符串”。 93 fetch: this.fetchApi, 93 获取:this.fetchApi,

This is an indication that you have two definitions for fetch .这表明您有两个fetch定义。 One is probably using the global fetch defined in the "dom" lib file, and the other is using one from some package (node-fetch?).一种可能是使用“dom”lib 文件中定义的全局fetch ,另一种是使用某个包中的一个(node-fetch?)。 You could try configuring the lib compiler option manually to exclude the dom types, which might resolve this conflict.您可以尝试手动配置lib编译器选项以排除 dom 类型,这可能会解决此冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM