Conflicting declaration merging doesn't fail in .d.ts files
#47 258 ouverte le 28 déc. 2021
Métriques du dépôt
- Stars
- (48 455 stars)
- Métriques de merge PR
- (Merge moyen 6j 17h) (9 PRs mergées en 30 j)
Description
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.