This package has been deprecated

Author message:

WARNING: This project has been renamed to ts-class-initializable. Install using ts-class-initializable instead.

@madsci/ts-class-initializable
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

ts-class-initializable

A small helper to enable class instance property initialization from a plain object.

Extend one of the Initializable classes and your class constructor will accept a plain object literal as its first argument, and assign the values to instance properties.

Example

class Foo extends Initializable<Foo> {
  public bar: string;
  public baz: number;
  public optional?: boolean;
}

const instance: Foo = new Foo({
  bar: 'foobar',
  baz: 2,
});

console.log(instance.bar); // 'foobar'

Classes

There are a few flavours:

  • Initializable: the constructor requires a plain object to initialize the instance
  • PartiallyInitializable: the constructor requires a plain object to initialize the instance, but all properties are treated as optional
  • OptionallyInitializable: the constructor may accept a plain object to initialize the instance, or you can do new Foo() without initialization
  • OptionallyPartiallyInitializable: the constructor may accept a plain object to initialize the instance, and all properties are treated as optional

Readme

Keywords

none

Package Sidebar

Install

npm i @madsci/ts-class-initializable

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

4.98 kB

Total Files

8

Last publish

Collaborators

  • sgarner