1.3.0 • Published 2 years ago

@thecodeaware/ts-interface-builder v1.3.0

Weekly downloads
7
License
MIT
Repository
github
Last release
2 years ago

Typescript Interface Builder

npm.io Build Status codecov MIT License npm.io npm.io npm.io npm.io

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

2 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago