microsoft / microsoft/TypeScript

Declaration merging can be inconsistent

Offen
#41,116 5 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@weswigham arbeitet bereits daran.

Seit 15.10.2020.

Needs Investigation Rescheduled
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

TypeScript Version: 4.0.3

Search Terms:

  • module
  • declare
  • merging
  • declaration

Code

Source file index.ts:

import { foo } from "./foo.ts"
console.log(foo({ fooField: "hello" }))

Source file foo.ts:

import { Bar } from "bar"

declare module "bar" {
  interface Bar {
    fooField?: string
  }
}

export const foo = (e: Bar) => e.fooField

Compiled index.d.ts:

export {};

Compiled foo.d.ts:

import { Bar } from "bar"
declare module "bar" {
    interface Bar {
        fooField?: string;
    }
}
export declare const foo: (e: Bar) => string | undefined;

Expected behavior:
Compiled index.d.ts should look like:

import "./foo"
export {};

The declaration merging for module "bar" should be happening when importing index.d.ts. More precicely:

The compiler should automatically replace import { x } from "./foo" with import "./foo" when generating .d.ts file, if no type from foo.d.ts is used/exposed but importing that file would result in module declarations merging.

Actual behavior:
index.d.ts does not import ./foo.d.ts, meaning that the declaration merging does not happen for module "bar".

Now if we do decide to export something from foo.ts, then everything just "works" (./foo.d.ts gets referenced from index.d.ts), which is wrong for several reasons:

  • we might not want to expose anything from foo
  • compilation errors might start occurring in the importing file (if it relied on Bar.fooField), because an unrelated export was removed
  • when developing index.ts, the dev will "see" the Bar.fooField property, and will wrongly assume that any user of that file will "see" it as-well.

The current way of supporting this is to manually import "./foo" at the beginning of index (in addition to the import { foo } from './foo'). But then it means that you need to know the internals of every dependency you have (you only need to explicitly import modules that do declare module definitions to be merged).

The compiler should automatically replace import { x } from "./foo" with import "./foo" inside .d.ts file, even if no type from foo.d.ts is used, if importing that file would result in a declaration merging.

Playground Link:
https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgMwhOBfFUIjgIgDoB6VCfAWACgBjCAOwGcIAbAU0JYgHMAKM3ggwBKYdWrFicYMjgkyhKIzh16MAIbAmcZiDZwAJmxot1UdTGAM4eqNy3cANHBgALNgE84AdwYByeAAjfXUwMBZgNgMfd3ppcGhLem4XV2BlZGB2IA

Related Issues:
N.A.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.