Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/equalogic/ts-class-initializable
TypeScript abstract class allowing instance property initialization from a plain object
https://github.com/equalogic/ts-class-initializable
class initialization typescript
Last synced: about 1 month ago
JSON representation
TypeScript abstract class allowing instance property initialization from a plain object
- Host: GitHub
- URL: https://github.com/equalogic/ts-class-initializable
- Owner: equalogic
- Created: 2019-07-04T18:42:26.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-10-14T03:18:11.000Z (3 months ago)
- Last Synced: 2024-10-15T18:03:42.901Z (3 months ago)
- Topics: class, initialization, typescript
- Language: TypeScript
- Size: 2.06 MB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ts-class-initializable
A small helper for TypeScript classes to enable instance properties to be initialized by passing
a plain object to the constructor - without every class needing boilerplate code to handle this.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.### Installation
```shell
npm i ts-class-initializable
```### Example
```ts
import { Initializable } from 'ts-class-initializable';class Foo extends Initializable {
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