Can not declaration merging for default exported class
See original GitHub issueTypeScript 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:
- Created 7 years ago
- Reactions:42
- Comments:21 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
file.ts
You can only augment “exported” declarations. Class
Foo
is exported asdefault
, and not asFoo
. so the nameFoo
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.