object-class-initializer
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

npm Build Status Sonar Quality Gate bundle size

Object Class Initializer

Demo repo for using an anonymous class as an object initializer in TypeScript.

Motivation 💥

Create an object using an anonymous class to access the 'object' during its initialization. It's 6 lines of code, you might want to just copy paste this instead of installing this package, but who am I to judge?

Features 🔥

✅ Create an object instance using an anonymous class

✅ Access the object during its initialization

✅ Type the object using generics (type safety)

✅ No dependencies

Built With 🔧

Installation 📦

pnpm i object-class-initializer
// or
yarn add object-class-initializer
// or
npm i object-class-initializer

Usage Example 🚀

import { createObject } from 'object-class-initializer';
const obj = createObject(
  class {
    a = 'foo';
    b = 'bar';
    c = this.a + this.b; // foobar
  }
);
// obj is now typed as { a: string, b: string, c: string }

// OR typed

type FooBar = {
  a: string;
  b: string;
  c: string;
};

const obj = createObject<FooBar>(
  class {
    a = 'foo';
    b = 'bar';
    c = this.a + this.b; // foobar
  }
);
// obj is now typed as FooBar

Contributing 🧑🏻‍💻

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License 🔑

Distributed under the MIT License. See LICENSE.txt for more information.

Contact 📧

Janik Schumacher - @LoaderB0T - linkedin

Project Link: https://github.com/LoaderB0T/object-class-initializer

Readme

Keywords

Package Sidebar

Install

npm i object-class-initializer

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

7.99 kB

Total Files

16

Last publish

Collaborators

  • loaderb0t