在TypeScript中,.d.ts声明文件中的 'export=' 是什么意思

下面的这种方式我可以理解,相当于一个模块暴露了多个接口。

declare module "abcde" {
    export let a: number
    export function b(): number
    export namespace c{
        let cd: string
    }
}

但是,下面这个就看不懂了,export = 是什么意思?

declare module "swiper" {
    const swiper: {
        new (element: Element | string, options?: SwiperOptions): Swiper;
    };

    export = swiper;
}
阅读 8.7k
1 个回答

Typescript (以下简称:ts) 有一个好处是,你可以将 ts 代码生成CommonJs规范、AMD规范,而这二者的规范中,且无法兼容,所以就有了 export =,将二者给统一,以至于让ts支持以上规范。

declare module "swiper" {
    const swiper: {
        new (element: Element | string, options?: SwiperOptions): Swiper;
    };

    export = swiper;
}

如果在CommonJS规范中:

const Swiper = require('swiper');

或者AMD规范中:

require(['swiper'], function(Swiper) {
    
});

当然,如果你的 @types 不需要支持上面两种规范,那就不需要 export =,因为对于 ts 而言,你只需要这样:

import { Swiper } from 'swiper'
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
Microsoft
子站问答
访问
宣传栏