Search code examples
typescriptsystemjscommonjsbabeljstypescript-typings

Can I import from TS 2.x typings written in TS 1.x?


I started learning Typescript couple days ago, it's been quite confusing so far, especially with modules.

I'm importing typings using npm install -s @types/knockout, but most of the typings availlable are still written in Typescript 1.

Here is a typing definition from es6-promise package:

declare module 'es6-promise' {
var foo: typeof Promise; // Temp variable to reference Promise in local context
namespace rsvp {
    export var Promise: typeof foo;
    export function polyfill(): void;
}
export = rsvp;
} 

Is there a way to import the default export specified as export = xxx from typescript 2?


Solution

  • Yes, you can use them. Unfortunately, TypeScript has adopted a backward looking approach to ES Modules instead of a forward looking one.

    export = rsvp
    

    is not a default export.

    A default export would have the form

    export default rsvp;
    

    export = is a TypeScript CommonJS specific construct that implies assignment to the module.exports property available of CommonJS modules.

    In order to import it as the default, you will need to do two things.

    1. set "allowSyntheticDefualtImports": true in your tsconfig.json
    2. use an ES Module <-> CommonJS Module interop aware runtime such as SystemJS and/or pipe your code through Babel in another build step