New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

ts-class-initializable

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-class-initializable

TypeScript abstract class enabling instance initialization from a plain object

latest
Source
npmnpm
Version
1.0.2
Version published
Weekly downloads
9.7K
13.02%
Maintainers
1
Weekly downloads
 
Created
Source

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

npm i ts-class-initializable

Example

import { Initializable } from 'ts-class-initializable';

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

FAQs

Package last updated on 04 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts