FG
๐ŸŒ Web & Full-StackMicrosoft

Duplicate type declarations with npm link

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

Using TypeScript 1.7.3. Suppose I have the below npm packages. The declaration files are generated by TypeScript compiler, and referred to from the other packages by means of the way described here. package-a ts src: [code block] ts declaration: [code block] package-b (depends on package-a): ts src: [code block] ts declaration: [code block] package-c (depends on package-a and package-b): ts src: [code block] The last line causes an error during compilation: [code block] When I remove the line `private foo;` from the declaration of package-a, TypeScript does not emit any error. However this workaround is a bit painful. I understand that exposing private properties to declaration is by design (https://github.com/Microsoft/TypeScript/issues/1532). I think TypeScript should ignore private properties when compiling variable assignment. Or is there any better workaround for this?

Error Output

error during compilation:

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Resolve Duplicate Type Declarations in TypeScript with npm link

Medium Risk

The error occurs because TypeScript includes private properties in the generated declaration files, which can lead to conflicts when multiple packages are linked together using npm link. When package-b or package-c attempts to reference package-a, the presence of private properties in the declaration can cause type conflicts during compilation.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Update TypeScript Version

    Upgrade TypeScript to a more recent version where improvements in type declaration handling have been made. This can help mitigate issues with private properties in declarations.

    bash
    npm install typescript@latest
  2. 2

    Modify Declaration Files

    If upgrading TypeScript is not an option, consider modifying the declaration files manually to exclude private properties. This can be done by creating a custom declaration file that omits private members.

    typescript
    // package-a.d.ts
    export declare class MyClass {
        public foo: string;
        // private foo; // Comment out or remove this line
    }
  3. 3

    Use TypeScript Compiler Options

    Adjust the TypeScript compiler options in your tsconfig.json to exclude private members from declaration files. This can be done by setting 'declaration' to true and 'emitDeclarationOnly' to true, ensuring only public members are emitted.

    json
    {
      "compilerOptions": {
        "declaration": true,
        "emitDeclarationOnly": true
      }
    }
  4. 4

    Rebuild Packages

    After making changes to the declaration files or updating TypeScript, ensure to rebuild all packages to reflect the changes. This can be done by running the build command in each package directory.

    bash
    npm run build
  5. 5

    Test Compilation

    Run the TypeScript compiler on package-c to ensure that the error is resolved and that there are no remaining type conflicts. This will confirm that the changes made have fixed the issue.

    bash
    tsc -p path/to/package-c/tsconfig.json

Validation

Confirm that the TypeScript compiler runs without errors after implementing the above steps. Additionally, verify that the expected types are correctly emitted in the declaration files and that no private properties are causing conflicts.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

typescriptcompilerbugfixed@types