Skip to content

Merged declarations are not written to the parent #32298

@fidian

Description

@fidian

TypeScript Version: 3.6.0-dev.20190704

Search Terms: merge declarations augment grafting

Code

parent.ts:

import { child1 } from './child1';

export class ParentThing implements ParentThing {}

// Add extra methods to this object.
child1(ParentThing.prototype);
// Imagine many more get added as well.

child1.ts:

import { ParentThing } from './parent';

declare module './parent' {
    interface ParentThing {
        add: (a: number, b: number) => number;
    }
}

export function child1(prototype: ParentThing) {
    prototype.add = (a: number, b: number) => a + b;
}

test.ts:

// Use this to test before compilation
import { ParentThing } from './parent';

// Use this to test after compilation
// import { ParentThing } from '../lib/parent';

const parentThing = new ParentThing();

console.log(parentThing.add(1, 1));

Expected behavior:

When tsc compiles the project, I should not lose the definitions. I want to be able to use the generated code as a module and still see the augmented type and be able to follow through to the methods that are defined by all children.

Several children are adding many methods to the parent.

Actual behavior:

Before compilation, there is no problem. I can jump to the definition when test.js is pointing to parent.ts. The declarations are merged and I can immediately access the add method in child1.

After compilation and when I change the test to load the compiled version, I get an error message: "Property 'add' does not exist on type 'ParentThing'."

Playground Link: None.

Related Issues: None that I could find.

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions