TypeScript export imported interface

后端 未结 3 925
日久生厌
日久生厌 2020-12-07 14:46

I use AMD modules and I want to hide a complex interface behind one file that loads several other files and chooses what to expose and how. It works, I use this solution but

相关标签:
3条回答
  • 2020-12-07 15:00

    There is an export import syntax for legacy modules, and a standard export format for modern ES6 modules:

    // export the default export of a legacy (`export =`) module
    export import MessageBase = require('./message-base');
    
    // export the default export of a modern (`export default`) module
    export { default as MessageBase } from './message-base';
    
    // when '--isolatedModules' flag is provided it requires using 'export type'.
    export type { default as MessageBase } from './message-base';
    
    // export an interface from a legacy module
    import Types = require('./message-types');
    export type IMessage = Types.IMessage;
    
    // export an interface from a modern module
    export { IMessage } from './message-types';
    
    0 讨论(0)
  • 2020-12-07 15:07

    Some more examples besides #c-snover's answer from here. You can put them together.

    import 'jquery';                        // import a module without any import bindings
    import $ from 'jquery';                 // import the default export of a module
    import { $ } from 'jquery';             // import a named export of a module
    import { $ as jQuery } from 'jquery';   // import a named export to a different name
    import * as crypto from 'crypto';       // import an entire module instance object
    
    export var x = 42;                      // export a named variable
    export function foo() {};               // export a named function
    
    export default 42;                      // export the default export
    export default function foo() {};       // export the default export as a function
    
    export { encrypt };                     // export an existing variable
    export { decrypt as dec };              // export a variable as a new name
    export { encrypt as en } from 'crypto'; // export an export from another module
    export * from 'crypto';                 // export all exports from another module
                                            // (except the default export)
    
    0 讨论(0)
  • 2020-12-07 15:09

    In my case particularly, I had to 'declare' the interface instead of exporting it.

    declare interface IFluxStoreSyncOptions{
      namespacedKey: string;
    }
    

    In order to use the interface as a type in another file like so:

    export function FluxStoreSync(options: IFluxStoreSyncOptions){
    }
    

    This way you don't have to export and import the interface.

    0 讨论(0)
提交回复
热议问题