简体   繁体   English

Typescript 未从模块的 index.d.ts 中找到类型

[英]Typescript not finding types from module's index.d.ts

Using Typescript 4.2.3, I am building a package from Typescript.使用 Typescript 4.2.3,我正在从 Typescript 构建 package。 When I install the package, its entry in node-modules has .js and .d.ts files, as expected.当我安装 package 时,它在node-modules中的条目有.js.d.ts文件,正如预期的那样。

├── dist
│   ├── index.d.ts
│   ├── index.js
│   ├── index.test.d.ts
│   └── index.test.js
└── package.json

Here is the content of the package.json :这是package.json的内容:

{
  "name": "postgres-base-class",
  "version": "0.1.3",
  "description": "Abstract class to handle an Postgres Client connection, provding execSql method to extending classes",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "devDependencies": {
<omitted>
  },
  "dependencies": {
    "pg": "^8.5.1"
  },
  "scripts": {
    "test": "jest",
    "prepack": "npm run clean && npm test && tsc --declaration",
    "clean": "tsc --build --clean"
  },
  "files": [
    "dist/"
  ],
  "author": "John Bito",
  "license": "UNLICENSED",
  "private": true
}

When I try to extend the class defined in the package, the protected method's parameters and return type are shown as any in VSCode, and tsc does not detect erroneous parameters.当我尝试扩展 package 中定义的 class 时,受保护方法的参数和返回类型在 VSCode 中显示为any ,并且tsc未检测到错误参数。 Instead, it appears to be treating the package as a JS package without types.相反,它似乎将 package 视为没有类型的 JS package。

What can I do to make the types in the package available to tsc where the package is installed?我该怎么做才能使安装了 package 的tsc可以使用 package 中的类型?

Here is the content of index.d.ts :这是index.d.ts的内容:

import { Client, QueryConfig, QueryResult } from "pg";
export type { QueryConfig } from "pg";
declare class ConnectURI {
    uriPromise: Promise<string>;
    uriString?: string;
    constructor(uri: Promise<string>);
    uri(): Promise<string>;
    toString(): string | undefined;
}
export default abstract class BasePostgresClass {
    connection?: Client;
    connectURI: ConnectURI;
    constructor(uri: Promise<string>);
    protected execSql(query: QueryConfig): Promise<QueryResult>;
    private connect;
    close(): Promise<void>;
}

The type of the execSql method where the package is imported seems to be (according to VSCode):导入 package 的execSql方法的类型似乎是(根据 VSCode):

execSql(query: any): Promise<any>

That matches the content of index.js produced by tsc (excerpted):这与tsc生成的index.js的内容相匹配(摘录):

class BasePostgresClass {
    constructor(uri) {
        this.connectURI = new ConnectURI(uri);
        console.debug("Got a promise for connection string");
    }
    async execSql(query) {
        console.debug("Executing query", query);

From what I can see, the pg dependency of the postgres-base-class dependency is getting shadowed by the pg dependency of the main project.从我所见, postgres-base-class依赖项的pg依赖项被主项目的pg依赖项所掩盖。

  • If your main project imports pg explicitly, add @types/pg to your main's devDependencies .如果您的主项目显式导入pg ,请将@types/pg添加到主项目的devDependencies中。
  • If not, ensure pg is NOT in the main project's dependencies (to avoid it shadowing the child's dependency types), and add @types/pg to your postgres-base-class [dev]dependencies如果不是,请确保pg不在主项目的依赖项中(以避免它影响子项目的依赖项类型),并将@types/pg添加到您的postgres-base-class [dev]dependencies

Working from yarn add../relativeyarn add../relative工作

Does pg contains its types build-in, or are you using a separate package for them in the devDependencies ? pg是否包含其内置类型,或者您是否在 devDependencies 中为它们使用单独的devDependencies If it is a separate package, probably you have to move it to dependencies .如果它是单独的 package,则可能您必须将其移至dependencies

The types defined in postgres-base-class/index.d.ts are expressed in terms of QueryConfig and QueryResult , which are not defined within the package, but are inherited from @types/pg . postgres-base-class/index.d.ts中定义的类型以QueryConfigQueryResult的形式表示,它们没有在 package 中定义,而是从@types/pg继承而来。 Since the package includes @types/pg as a devDependency , the package of type definitions is not installed in the package that installs postgres-base-class as a dependency.由于 package 包含@types/pg作为devDependency ,因此类型定义的 package 未安装在postgres-base-class中作为依赖项安装。 By default, tsc acts as if the missing type definitions were any .默认情况下, tsc就像缺少的类型定义是any一样。

The easy solution is to add @types/pg to the dependencies of postgres-base-class .简单的解决方案是将@types/pg添加到postgres-base-classdependencies项中。 A better solution (that would properly isolate users from the details of the pg package) would be to define distinct types for postgres-base-class .更好的解决方案(将用户与pg包的细节正确隔离)是为postgres-base-class定义不同的类型。

One remaining question is whether tsc can be configured to emit a warning/error that a type referenced in an imported package is undefined.剩下的一个问题是tsc是否可以配置为发出警告/错误,即在导入的 package 中引用的类型未定义。

The moral of the story is that it really is a benefit when packages include their types instead of relying on separate @types packages.这个故事的寓意是,当包包含它们的类型而不是依赖于单独的@types包时,它确实是一个好处。

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

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