1

Let's say I have a declarations file foo.d.ts:

declare namespace foo {
  interface Bar {
    (): void;
  }
}

declare var foo: foo.Bar;
export default foo;

If I compile this:

import Foo from './foo';
Foo();

The resulting output is:

"use strict";
var foo_1 = require('./foo');
foo_1["default"]();

However this code won't run as foo_1 is a function and has no property default. How can I get the output to be foo_1() instead of foo_1["default"]()?

3
  • have you configured a module in the tsconfig.json file? You probably shouldn't in your case. Commented Jan 10, 2017 at 15:20
  • No, this is intended to be a declarations file to accompany a library. Commented Jan 10, 2017 at 15:26
  • you should never have a declare namespace foo {} and a declare var foo in the same file. try renaming one of those. Commented Dec 22, 2018 at 0:14

1 Answer 1

3

Use

export = foo;

instead of export default foo; in your declaration file

and use import require when importing:

import Foo = require('./foo');

Export assignment/import require is special syntax in typescript for dealing with node modules with exports like

module.exports = function someFunction() {}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.