簡體   English   中英

Typedoc / Typescript 編譯器在導入的類型上拋出錯誤

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

概括

我正在嘗試使用 Typedoc 為 TypeScript 項目構建文檔。 不幸的是,Typedoc 在我在其中一個文件中使用的導入語句中產生了錯誤。

我很困惑,因為我的項目中沒有使用它進行錯誤檢查的文件,或者構造我的導入類型所必需的。

問題前言

我正在導入一個類型,使用語法import type { Type } 具體來說,這個文件export是一個interface ,我在 import 語句中使用相對路徑(在我的項目根目錄之外)來訪問它。 該文件是使用 openapi-generator 生成的模型。 這個文件的頂部還有一堆import語句,其中一個叫做runtime.ts

Typedoc 和我的 TSConfig 似乎正在解析所有這些導入,然后對它們進行錯誤檢查。 當我嘗試運行 Typedoc 時,來自runtime.ts的錯誤出現在我的控制台中。 這是令人困惑的行為; 我本來希望我的導入語句包括我正在導入的文字類型,以及屬性所需的枚舉。

我試過的

我嘗試省略 type 關鍵字(以便 import 語句讀取import { Type } ),但無濟於事。 該接口是准系統:一個字符串屬性,兩個枚舉。 我正在辯論只是將接口和枚舉定義復制到我的項目文件中,但這是一個臨時修復,不是可擴展的解決方案。

語境

我的 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"],

}

我的 typedoc.json ......

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

非常感謝任何幫助或指導! 感謝您的閱讀。

我本來希望我的導入語句只包括我正在導入的文字類型,以及屬性所需的枚舉。

不幸的是,這是一個非常普遍的誤解。 這不是編譯器 API 的工作方式,因此也不是 TypeDoc 的工作方式。 如果您使用npx tsc -p tsconfig.json編譯項目,您將收到相同的錯誤。

runtime.ts:93:21 - 錯誤 TS2322:類型 '(url: string, init: RequestInit) => Promise' 不可分配給類型 '{ (input: RequestInfo, init?: RequestInit | undefined): Promise; (輸入:RequestInfo, init?: RequestInit | undefined): Promise<...>; }'。 參數 'url' 和 'input' 的類型不兼容。 類型“RequestInfo”不可分配給類型“字符串”。 類型“請求”不可分配給類型“字符串”。 93 獲取:this.fetchApi,

這表明您有兩個fetch定義。 一種可能是使用“dom”lib 文件中定義的全局fetch ,另一種是使用某個包中的一個(node-fetch?)。 您可以嘗試手動配置lib編譯器選項以排除 dom 類型,這可能會解決此沖突。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM