Microsoft/TypeScript

Conflicting declaration merging doesn't fail in .d.ts files

Open

#47,258 opened on 2021幎12月28日

GitHub で芋る
 (0 comments) (0 reactions) (0 assignees)TypeScript (6,726 forks)batch import
BugDomain: BinderHelp Wanted

Repository metrics

Stars
 (48,455 stars)
PR merge metrics
 (平均マヌゞ 6d 17h) (30d で 9 merged PRs)

説明

Bug Report

🔎 Search Terms

declaration merging

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about declaration merging

(NOTE: I only tried this on TS 4.5.4 because I couldn't reproduce the issue in the bug workbench; see below.)

⏯ Playground Link

Repro here. I tried to reproduce the error in bug-workbench but the error is flagged correctly there. (Although, I did discover https://github.com/microsoft/TypeScript-Website/issues/2198 in the process...)

💻 Code

//@filename: second.d.ts
export {}

declare global {
    interface Foo {
        x: number;
    }
}


// @filename: first.d.ts
export {}

declare global {
    interface Foo {
        x: string;
    }
}





// @filename: index.ts
import "second";
import "first";

declare const f: Foo;

f.x = "";
f.x = 2;

🙁 Actual behavior

Running tsc on the repro does not flag the conflicting interfaces as an error. One interface is chosen over the other depending on which one reaches the typechecker first. (Check out the repro and change the order of the import statements to see what I mean.)

🙂 Expected behavior

The compiler should flag the second imported interface declaration as invalid and refuse to emit, rather than silently ignoring one of the declarations.

Alternately, if this is correct behavior, it should be explained in the relevant section of the Handbook, which currently says

Non-function members of the interfaces should be unique. If they are not unique, they must be of the same type. The compiler will issue an error if the interfaces both declare a non-function member of the same name, but of different types.

コントリビュヌタヌガむド