🌀
type-chef-di Docs
GitHub
  • 👋Welcome to type-chef-di documentation
  • Overview
    • ✨Getting started
  • Fundamentals
    • 🍎Container Options, extendability
    • 👨‍🍳Injection
      • 🍕Type injection
      • 🍪Token registration
      • ☕Token Injection
      • 🌮Mixed injection
    • 🛠️Decorators
      • ✅@Injectable({instantiation: "singleton" | "prototype"})
      • 🎯@Inject<T>(key: string | Type<T>)
      • 🍍@InjectProperty<T>(key: string | Type<T>)
      • 🍭@Setter()
      • 🔆@InitMethod()
      • 🏭@FactoryMethod()
      • ⚡@AddTags(tags)
      • 🪃@RunBefore(key: string | Type<IRunBefore>)
      • 🛴@RunAfter(key: string | Type<IRunAfter>)
      • 🌕@MethodWrapper(key: string | Type<IMethodWrapper>)
  • Use Cases
    • 🎨Plans
    • 🖥️For Developers
  • Other
    • 💡DI / IoC
Powered by GitBook
On this page
Edit on GitHub
  1. Fundamentals
  2. Decorators

@InjectProperty<T>(key: string | Type<T>)

Basically the same as @Inject<T>(key: string | Type<T>) but with class properties.

interface IFoo {
    getNumber(): number;
}

class Foo implements IFoo {
   getNumber() {
       return 69;
   }
}

class Foo2 implements IFoo {
    getNumber() {
        return 420;
    }
}
        
class Test {
        @InjectProperty('value')
        testProp: any;
        
        @InjectProperty('value2')
        testProp2: any;
        
        
        @InjectProperty<IFoo>(Foo)
        testProp!: IFoo; // 69

        @InjectProperty<IFoo>(Foo2)
        testProp2!: IFoo; //420
        
        constructor() {
        }
    }
    container.register('value', 'happy');
    container.register('value2', 'panda');
    container.register('test', Test);
    const testObj = await container.resolve<Test>('test');
    console.log(`I am a ${testObj.testProp} ${testObj.testProp2}`) // result: I am a happy panda
Previous@Inject<T>(key: string | Type<T>)Next@Setter()

Last updated 2 years ago

🛠️
🍍