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: 
          - 
            propertyName:   x
            typeAnnotation: 
              typeName:      Any
              typeArguments: 
                (empty array)
              isArray:       false
        typeParameters: null
        heritages: 
          implementList: null
          extend:        null
    properties: 
      - 
        typeAnnotation: 
          annotationType: functionType
          returnType: 
            typeName:      Void
            typeArguments: 
              (empty array)
            isArray:       false
          arguments: 
            - 
              identifierName: str
              typeAnnotation: 
                typeArguments: 
                  (empty array)
                isArray:       true
              nullable:       true
        typeParameters: null
      - 
        propertyName:   a
        typeAnnotation: 
          typeName:      Number
          typeArguments: 
            (empty array)
          isArray:       false
    interfaces: 
      (empty array)
classes: 
  - 
    className:      X
    properties: 
      (empty array)
    typeParameters: null
    heritages: 
      implementList: null
      extend:        null
properties: 
  - 
    propertyName:   x
    typeAnnotation: 
      typeName:      Any
      typeArguments: 
        (empty array)
      isArray:       false
interfaces: 
  - 
    interfaceName:  IFoo
    properties: 
      - 
        propertyName:   s
        typeAnnotation: 
          typeName:      String
          typeArguments: 
            (empty array)
          isArray:       false
      - 
        propertyName:   f
        typeAnnotation: 
          annotationType: lambdaFunctionType
          typeAnnotation: 
            typeName:      Object
            typeArguments: 
              (empty array)
            isArray:       true
          arguments: 
            - 
              identifierName: t
              typeAnnotation: 
                typeName:      Number
                typeArguments: 
                  (empty array)
                isArray:       false
              nullable:       false
    typeParameters: null
    heritages: 
      implementList: null
      extend:        null
  - 
    interfaceName:  IFoo2
    properties: 
      - 
        propertyName:   n
        typeAnnotation: 
          typeName:      Number
          typeArguments: 
            (empty array)
          isArray:       false
    typeParameters: null
    heritages: 
      implementList: null
      extend: 
        typeName:      IFOO
        typeArguments: 
          (empty array)
        isArray:       false

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

dts-parser dummy.d.ts --json

誰が使うの

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

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

苦労

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