@thecodeaware/ts-interface-builder
v1.3.0
Published
Builder pattern for typescript types.
Downloads
16
Maintainers
Readme
Typescript Interface Builder
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
- Why not API with
with
prefix likewithLabel
forlabel
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
andLabel
.