【问题标题】:Typescript export both class and types打字稿导出类和类型
【发布时间】:2017-05-01 21:09:31
【问题描述】:

所以我为一个模块创建了自己的类型定义:

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;
...

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

【问题讨论】:

    标签: typescript


    【解决方案1】:

    仔细查看@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;
    }
    

    正是我要找的东西

    【讨论】:

    • 是的,这就是要走的路。很多 DT 类型应该更改为这种格式。 :)
    • 顺便说一句,为什么他们不使用声明模式部分?
    • github.com/types* 不是npm install @types/*npm install @types/*来自github.com/definitelytyped/definitelytyped
    • github.com/types 中的 typings 用于typings 和将来的github.com/Microsoft/types-publisher/issues/4 这些以外部模块格式编写,因为模块名称是已知的。这是一个更好的 IMO 解决方案,因为模块可以映射到不同的名称,并且它支持多个模块解析(嵌套模块)。
    • 目前,DT 中的类型不支持,这在 IMO 中是一个相当大的问题。但是,github.com/Microsoft/types-publisher/issues/4 存在一年,仍未解决。直接与 DT 打交道并不容易。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 2022-01-22
    • 2017-09-15
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    相关资源
    最近更新 更多