Skip to content

Non-visible implementations produce invalid declaration file #48525

Closed

Description

Bug Report

🔎 Search Terms

declaration, implementation

🕗 Version & Regression Information

  • This changed between versions 2.8.4 and 2.9.1

Note: Prior to 2.9, TypeScript does not permit exported types to reference non-exported types. From 2.9 on, TypeScript permits it, but (as described here) generates invalid declaration files.

⏯ Playground Link

Playground link

However, oddly, the playground does not reproduce this bug. (Which itself is perhaps a secondary issue.)

💻 Code

example.ts

export interface Foo {
    item: Bar;
}

interface Bar {
  baz(): void;
}

namespace Bar {
  export function biz() {
    return 0;
  }
}

Run

tsc --declaration example.ts

🙁 Actual behavior

example.d.ts

export interface Foo {
    item: Bar;
}
interface Bar {
    baz(): void;
}
declare namespace Bar {
    function biz() {
        return 0;
    }
}
export {};

Note that this declaration file is invalid because biz() has an implementation. Attempting to use this declaration file yields:

error TS1183: An implementation cannot be declared in ambient contexts.

🙂 Expected behavior

I expect the produced declaration file to omit all implementations:

example.d.ts

export interface Foo {
    item: Bar;
}
interface Bar {
    baz(): void;
}
declare namespace Bar {
    function biz(): number;
}
export {};

Alternatively, I expect TypeScript to produce an error.

Alternatively, I expect TypeScript to accept implementations in declaration files.

But in no situation do I expect TypeScript to generate a declaration file that it itself does not accept.

Activity

  1. changed the title [-]Non-visible implementations included in declarations[/-] [+]Non-visible implementations produce invalid declaration file[/+] on Apr 2, 2022
  2. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: Declaration EmitThe issue relates to the emission of d.ts filesFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions