question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can not declaration merging for default exported class

See original GitHub issue

TypeScript Version: 2.1.5

Code

// A *self-contained* demonstration of the problem follows...
// ===== file Foo.ts =====
export default class Foo {
  hello() { 
    console.log('Foo'); 
  }
}


// ===== file Bar.ts =====
import Foo from './Foo';

declare module './Foo' {
  interface Foo {
    add(x, y);
  }
}

// ERROR: 'add' does not exist in type Foo.
Foo.prototype.add = function (x, y) { return x + y; };

let f = new Foo();
f.hello();
f.add(3, 4);   // ERROR: 'add' does not exist in type Foo.

Expected behavior: If the class Foo is exported without default in file Foo.ts, and then import {Foo} from ‘./Foo’, it works correctly as example in doc http://www.typescriptlang.org/docs/handbook/declaration-merging.html

Hope import Foo same as import {Foo} behaviors.

Actual behavior: ERROR message showed.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:42
  • Comments:21 (5 by maintainers)

github_iconTop GitHub Comments

14reactions
rdsedmundocommented, Aug 15, 2019

Is there a way for augmenting a module exported with export = though? I wasn’t able to get it working.

i.e

knex.d.ts

interface Knex {
  test1: string;
}

export = Knex;

file.ts

declare module 'knex' {
  interface Knex {
    test2: string;
  }
}
8reactions
mhegazycommented, Feb 15, 2017

You can only augment “exported” declarations. Class Foo is exported as default, and not as Foo. so the name Foo does not exist outside the module.

It just happens that default is a reserved word, and can not used as an interface declaration name.

The TS compiler needs to allow export { Foo as default} in module augmentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Declaration Merging - TypeScript
Module Augmentation · You can't declare new top-level declarations in the augmentation — just patches to existing declarations. · Default exports also cannot...
Read more >
Declaration merging in TypeScript for regular devs - Merixstudio
Ready to try and merge TypeScript declarations the easy way for your projects? ... Also, it is not possible to augment default exports....
Read more >
Write a declaration file for a default export module
You're really close. Instead of using export default , you should use export = . custom-typings/rivescript.d.ts declare module 'rivescript' ...
Read more >
no-unsafe-declaration-merging - TypeScript ESLint
Declaration merging between classes and interfaces is unsafe. The TypeScript compiler doesn't check whether properties are initialized, which can cause lead ...
Read more >
Merging Declarations in TypeScript | by John Au-Yeung
With TypeScript, we can declare many entities that don't exist in ... All the exported interfaces and classes are merged into one namespace....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found