typescript 没有从模块的 index.d.ts 中找到类型

标签 typescript

使用 Typescript 4.2.3,我正在从 Typescript 构建一个包。当我安装软件包时,它在 node-modules 中的条目有 .js.d.ts文件,正如预期的那样。

├── dist
│   ├── index.d.ts
│   ├── index.js
│   ├── index.test.d.ts
│   └── index.test.js
└── 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
}
当我尝试扩展包中定义的类时, protected 方法的参数和返回类型显示为 any在 VSCode 和 tsc不检测错误参数。相反,它似乎将包视为没有类型的 JS 包。
我该怎么做才能使包中的类型对 tsc 可用软件包安装在哪里?
这里是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>;
}
execSql 的类型导入包的方法似乎是(根据VSCode):
execSql(query: any): Promise<any>
index.js 的内容相匹配由 tsc 制作(摘录):
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);

最佳答案

据我所知,pg postgres-base-class 的依赖关系依赖被 pg 所掩盖主项目的依赖。

  • 如果您的主项目导入 pg明确地,添加 @types/pg到你的主devDependencies .
  • 如果没有,请确保 pg不在主项目的依赖项中(以避免它影响子项目的依赖项类型),并添加 @types/pg给您的postgres-base-class [开发] 依赖

  • Working from yarn add ../relative

    关于 typescript 没有从模块的 index.d.ts 中找到类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66709908/

    相关文章:

    typescript - 类型 'null' 的参数不可分配给类型 'TableContinuationToken' 的参数

    javascript - 选择框不首先显示所选选项

    typescript - 传递 typescript 类方法作为回调

    reactjs - React Monorepo yarn 工作区 + typescript + 绝对导入

    reactjs - 将联合类型传递给 React 的 useRef 不起作用

    javascript - Angular4/Typescript 中的 Angular ForEach?

    html - Angular2 NgStyle 中的两个函数

    typescript - 如何在 TypeScript 中严格定义对象值的子类类型

    Angular 10 : Date range picker - calender opens at wrong place

    angular - D3.js V4 和 Angular 不会发生节点转换