TypeDI
Github
Development Version
Development Version
  • Old documentation
  • Getting Started
  • Usage Guide
    • Container API
    • @Service decorator
    • @Inject decorator
    • Service Tokens
    • Inheritance
    • Usage with TypeORM
  • Advanced Usage
    • Creating custom decorators
    • Using scoped container
    • Transient services
  • Usage without TypeScript
    • Getting Started
    • Usage
      • Old documentation
Powered by GitBook
On this page

Was this helpful?

  1. Usage Guide

Inheritance

Inheritance is supported for properties when both the base and the extended class is marked with the @Service() decorator. Classes which extend a class with decorated properties will receive the initialized class instances on those properties upon creation.

import 'reflect-metadata';
import { Container, Token, Inject, Service } from 'typedi';

@Service()
class InjectedClass {
  name: string = 'InjectedClass';
}

@Service()
class BaseClass {
  name: string = 'BaseClass';

  @Inject()
  injectedClass: InjectedClass;
}

@Service()
class ExtendedClass extends BaseClass {
  name: string = 'ExtendedClass';
}

const instance = Container.get(ExtendedClass);
// instance has the `name` property with "ExtendedClass" value (overwritten the base class)
// and the `injectedClass` property with the instance of the `InjectedClass` class

console.log(instance.injectedClass.name);
// logs "InjectedClass"
console.log(instance.name);
// logs "ExtendedClass"
PreviousService TokensNextUsage with TypeORM

Last updated 3 years ago

Was this helpful?