使用 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/