Typescript导出类和类型

时间:2017-05-01 21:09:32

标签: typescript

所以我为模块创建了自己的类型def:

declare module 'fs-tree-diff' {
  interface FSEntry {
    relativePath: string;
    mode: number;
    size: number;
    mtime: number;
    isDirectory: () => boolean;
  }

  type Operation = 'unlink' | 'create' | 'mkdir' | 'change' | 'rmdir';
  type PatchEntry = [Operation, string, FSEntry];
  type Patch = PatchEntry[];

  type FSEntries = FSEntry[];

  class FSTree {
    constructor (options: {entries: FSEntries, sortAndExpand?: boolean});
    static fromEntries (entries: FSEntries): FSTree;
    static fromPaths (paths: string[]): FSTree;
    addEntries (entries: FSEntries, options: {sortAndExpand: boolean}): void;
    addPath (paths: string[], options: {sortAndExpand: boolean}): void;
    forEach (fn: any, context: any): void;
    calculatePatch (other: FSTree, isEqual?: (a: FSEntry, b: FSEntry) => boolean): Patch;
  }

  export = FSTree;
}

现在,我可以在任何地方这样做:

import FSTree = require('fs-tree-diff');

const tree = new FSTree({entries: []});

它有效!但我现在想能够做到

import FSTree = require('fs-tree-diff');

const tree = new FSTree({entries: []});
let entry: FSTree.FSEntry;
...

如果我尝试在我的模块声明中的每个export之前添加type,它会在export = ...的末尾说明它无法与其他导出一起导出。如何从其他文件访问我的defs?

1 个答案:

答案 0 :(得分:0)

仔细检查@types/bluebird后,我发现:

declare module 'fs-tree-diff' {
  namespace FSTree {
    interface FSEntry {
      relativePath: string;
      mode: number;
      size: number;
      mtime: number;
      isDirectory: () => boolean;
    }

    type Operation = 'unlink' | 'create' | 'mkdir' | 'change' | 'rmdir';
    type PatchEntry = [Operation, string, FSEntry];
    type Patch = PatchEntry[];

    type FSEntries = FSEntry[];
  }

  class FSTree {
    constructor (options: {entries: FSTree.FSEntries, sortAndExpand?: boolean});
    static fromEntries (entries: FSTree.FSEntries): FSTree;
    static fromPaths (paths: string[]): FSTree;
    addEntries (entries: FSTree.FSEntries, options: {sortAndExpand: boolean}): void;
    addPath (paths: string[], options: {sortAndExpand: boolean}): void;
    forEach (fn: any, context: any): void;
    calculatePatch (other: FSTree, isEqual?: (a: FSTree.FSEntry, b: FSTree.FSEntry) => boolean): FSTree.Patch;
  }

  export = FSTree;
}

是我一直在寻找的