Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use this instead #247

Open
Prozi opened this issue Oct 6, 2024 · 0 comments
Open

use this instead #247

Prozi opened this issue Oct 6, 2024 · 0 comments

Comments

@Prozi
Copy link

Prozi commented Oct 6, 2024

https://www.npmjs.com/package/inject.min

this is super lightweight 5kb typescript

import { Inject } from 'inject.min';

class Example {
  value: string;

  constructor(props?: { param: string }) {
    this.value = props?.param || 'example';
  }
}

class Example2 extends Example {
  constructor(param: string) {
    super({ param });
  }
}

class Test {
  @Inject(Example) example!: Example;
  @Inject(Example2, 'example2') example2!: Example2;
  @Inject(Example, { param: 'example3' }) example3!: Example;

  constructor() {
    console.log(this.example.value); // example
    console.log(this.example2.value); // example2
    console.log(this.example3.value); // example3
  }
}

class Test2 {
  @Inject(Example) example!: Example;
  @Inject(Example2, 'different') example2!: Example2;
  @Inject(Example, { param: 'example3' }) example3!: Example;

  constructor() {
    console.log(this.example.value); // example
    console.log(this.example2.value); // different
    console.log(this.example3.value); // example3
  }
}

class Test3 extends Test {}

const test = new Test(); // example, example2, example3
const test2 = new Test2(); // example, different, example3
const test3 = new Test3(); // example, example2, example3

console.log(test.example === test2.example); // true
console.log(test.example2 === test2.example2); // false
console.log(test.example2 === test3.example2); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants