0.3.1 • Published 4 years ago

@hulkstack/ts-builder v0.3.1

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

@hulkstack/ts-builder

Utility wrapper for easing the programmatic usage of typescript's compiler.

Getting started

Install using your preferred package manager

npm install @hulkstack/ts-builder --save-dev

Usage

Basic example:

import {
  Interface,
  Export,
  Property,
  Identifier,
  TypeParam,
  ArrayType,
  StringType,
  UnionType,
  Optional,
  RefType,
  TypescriptBuilder
} from "@hulkstack/ts-builder";

const interfaceNode = Interface(
  Export(),
  Identifier("SomeInterface"),
  TypeParam(
    Identifier("T"),
    StringType()
  ),
  Property(
    Identifier("propertyOne"),
    ArrayType(
      UnionType(
        StringType(),
        NumberType()
      )
    )
  ),
  Property(
    Identifier("propertyTwo"),
    Optional(),
    RefType(Identifier("SomeInterface"))
  )
)

const builder = new TypescriptBuilder([
  interfaceNode
], { fileName: "testing.ts" })

builder.visit({
  // Visit all interface declarations
  InterfaceDeclaration (node) {
    // Change the name of all declared interfaces
    node.name = Identifier(node.name.text + "New")
  }
})

console.log(builder.print())

Output:

export interface SomeInterface<T extends string> {
  propertyOne: (string | number)[];
  propertyTwo?: SomeInterface;
}

Somewhat verbose yes, but less verbose and more proficient than directly using the factory functions from typescript.

0.3.1

4 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago