TypeScriptのd.tsパースして型情報を抜き出すの作った

土日使って気合で作った。

mizchi/dts-parser

とりあえずlib.d.tsがパースできたのでだいたいのものはパースできると思う。

パース結果のファイルサイズが大きすぎてバイナリ扱いされてて笑った。 https://raw.githubusercontent.com/mizchi/dts-parser/master/lib.d.ts.parsed

使い方

こんな型定義dummy.d.tsが

declare module Foo {
    export function fun(str: string):void;
    export function funfun(str: string):void;
    export var a:number, b: Object;
    export var c: Object;
    export class Bar {
        f():string;
        x: any;
    }
}

export class X {}
export var x: any;

こんな風に出力されます。

$ npm install
$ ./bin/dts-parser examples/dummy.d.ts
moduleName: Top
modules:
  -
    moduleName: Foo
    modules:
      (empty array)
    classes:
      -
        className:  Bar
        properties:
          -
            typeAnnotation:
              annotationType: functionType
              returnTypeName: String
              arguments:
                (empty array)
          -
            propertyName:   x
            typeAnnotation:
              nodeType: identifierType
              typeName: Any
    properties:
      -
        propertyName:   fun
        typeAnnotation:
          annotationType: functionType
          returnTypeName: Void
          arguments:
            -
              identifierName: str
              typeAnnotation: String
      -
        propertyName:   funfun
        typeAnnotation:
          annotationType: functionType
          returnTypeName: Void
          arguments:
            -
              identifierName: str
              typeAnnotation: String
      -
        propertyName:
          - a
        typeAnnotation:
          nodeType: identifierType
          typeName: Number
      -
        propertyName:
          - b
        typeAnnotation:
          nodeType: identifierType
          typeName: Object
      -
        propertyName:
          - c
        typeAnnotation:
          nodeType: identifierType
          typeName: Object
classes:
  -
    className:  X
    properties:
      (empty array)
properties:
  -
    propertyName:
      - x
    typeAnnotation:
      nodeType: identifierType
      typeName: Any

-j, --json でただのjsonとして出力できます

dts-parser dummy.d.ts --json

誰が使うの

altjs作者 or 補完エンジン作者

というか、TypedCoffeeScriptの為に作りました。jsxとかscala.jsとかあのへんでも頑張れば何かに使えると思います。

苦労

TypeScript AST が辛くてスクレイピング気味なコードなのでバージョン変わってAST少しでも変わると死にそう。