Skip to content

LoaderB0T/object-class-initializer

Repository files navigation

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?

Read About This 📖

I wrote a medium post about the idea of this package. Please read the post to get all the details!

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •