'Typedoc / Typescript Compiler throws error on an imported type
Summary
I am attempting to build documentation for a TypeScript project using Typedoc. Unfortunately, Typedoc is yielding errors on an import statement I use in one of my files.
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 }
. 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. This file is a model generated using openapi-generator. This file also has a bunch of import
statements at the top, one of which is called runtime.ts
.
Typedoc and my TSConfig appear to be resolving all of these imports and then error-checking them. Errors from runtime.ts
are what appear in my console when I attempt to run Typedoc. 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. 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...
{
"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...
{
"entryPoints": ["./src/content.ts"],
"out": "./doc",
"tsconfig": "./tsconfig.json",
}
Any help or guidance is much appreciated! Thank you for reading.
Solution 1:[1]
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. If you compile your project with npx tsc -p tsconfig.json
, you will receive the same errors.
runtime.ts:93:21 - error TS2322: Type '(url: string, init: RequestInit) => Promise' is not assignable to type '{ (input: RequestInfo, init?: RequestInit | undefined): Promise; (input: RequestInfo, init?: RequestInit | undefined): Promise<...>; }'. Types of parameters 'url' and 'input' are incompatible. Type 'RequestInfo' is not assignable to type 'string'. Type 'Request' is not assignable to type 'string'. 93 fetch: this.fetchApi,
This is an indication that you have two definitions for 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?). You could try configuring the lib
compiler option manually to exclude the dom types, which might resolve this conflict.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gerrit0 |