object-class-initializer
v1.0.1
Published
Create an object using an anonymous class to access the 'object' during its initialization.
Downloads
5
Readme
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!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - 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