John
John

Reputation: 1351

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

Using Typescript 4.2.3, I am building a package from Typescript. When I install the package, its entry in node-modules has .js and .d.ts files, as expected.

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

Here is the content of the 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. Instead, it appears to be treating the package as a JS package without types.

What can I do to make the types in the package available to tsc where the package is installed?

Here is the content of 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):

execSql(query: any): Promise<any>

That matches the content of index.js produced by tsc (excerpted):

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);

Upvotes: 5

Views: 3706

Answers (2)

KingGary
KingGary

Reputation: 638

Does pg contains its types build-in, or are you using a separate package for them in the devDependencies? If it is a separate package, probably you have to move it to dependencies.

Upvotes: 1

Andr&#233; Matos
Andr&#233; Matos

Reputation: 11

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.

  • If your main project imports pg explicitly, add @types/pg to your main's 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

Working from yarn add ../relative

Upvotes: 1

Related Questions