Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@thecodeaware/ts-interface-builder

thecodeaware51MIT1.3.0TypeScript support: included

Builder pattern for typescript types.

typescript, interface, types, builder, pattern, proxy

readme

Typescript Interface Builder

Build Status codecov MIT License

Builder pattern for typescript types.

Installation


npm install @thecodeaware/ts-interface-builder

Usage

import { builderOf } from '@thecodeaware/ts-interface-builder';

interface Input {
  label: string;
  value: number;
  title?: string;
}

const input: Input = builderOf<Input>().title('title').label('label').value(2).build();

// with default object
const inputWithDefaults: Input = builderOf<Input>({
  title: 'defaultTitle',
  label: 'defaultLabel',
  value: 1,
})
  .title('title')
  .value(2)
  .build();

Contribution

Feel free to add improvements. Remember about the tests!

npm install
npm run test

FAQ

  1. Why not API with with prefix like withLabel for label property?

It is possible with TS but it brings more edge cases.

export type TypeBuilder<T> = {
  [P in keyof T as `with${Capitalize<string & P>}`]: (arg: T[P]) => TypeBuilder<T>;
} & {
  build(): T;
};
  • Object with capitalized property.
  • Object with capitalized and non-capitalized property like label and Label.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[1.3.0] 2023-07-29

Added

  • Added prettier configuration.
  • Added nvmrc configuration (node 18.17)

Changed

  • Changed builder type name.
  • Bump dependencies versions.
  • Replace tslint with eslint.

Fixed

  • Fixed undefinied issue for optional properties.

Removed

  • Removed support for ES5.

[1.2.0] - 2019-03-28

Added

  • Standardized a changelog (use Keep a Changelog 1.0.0).
  • Add Keep a Changelog badge.
  • Add Semantic Versioning badge.

Changed

  • Add missing changelog logs.
  • Enable unit test for jsdom and node.

[1.1.0] - 2018-12-31

Added

  • Add defaults option to es6 builder.

[1.0.4] - 2018-12-27

Changed

  • Only PR branch and master should trigger travis build.
  • Clean CI travis build configuration.

[1.0.3] - 2018-12-24

Added

  • Add codecov.

Fixed

  • Add missing travis file to npm ignore.

[1.0.2] - 2018-12-23

Added

  • Add Travis CI build.

[1.0.1] - 2018-12-23

Added

  • Add badges in Readme file.

[1.0.0] - 2018-12-06

Added

  • Add shortcut import to es5 version.

Changed

  • Fix catalogues inconsistency.
  • Give better names for es5 and es6 builders.

Migration

  • Change import { builderOf } from '@thecodeaware/ts-interface-builder/dist/simple/SimpleInterfaceBuilder'; to import { builderOf } from '@thecodeaware/ts-interface-builder/dist/es5';