Skip to content

InstanceLink

reference
1 min readUpdated

Kind: Interface

Source: packages/core/injector/instance-links-host.ts

Part of: Core

InstanceLink describes a resolved dependency entry managed by the injector. It connects an injection token to its InstanceWrapper, the module-level wrapper collection where it was found, and the owning moduleId for module-aware resolution.

Properties

PropertyType
tokenInjectionToken
wrapperRefInstanceWrapper<T>
collectionMap<any, InstanceWrapper>
moduleIdstring

Diagram

mermaid
graph LR
  Token[InjectionToken] --> Link[InstanceLink]
  Link --> Wrapper[InstanceWrapper<T>]
  Link --> Collection[Map<any, InstanceWrapper>]
  Link --> ModuleId[moduleId: string]
  Collection --> Wrapper

Usage

ts
import { InstanceLink } from './instance-links-host';
import { InstanceWrapper } from './instance-wrapper';

function inspectLink<T>(link: InstanceLink<T>) {
  console.log(`Resolved token from module: ${link.moduleId}`);
  console.log('Token:', link.token);
  console.log('Wrapper:', link.wrapperRef);

  // The collection contains all provider wrappers for the owning module.
  const registeredWrapper = link.collection.get(link.token);

  return registeredWrapper ?? link.wrapperRef;
}

// Typically created internally by the injector during dependency resolution.
const link: InstanceLink<MyService> = {
  token: MyService,
  wrapperRef: myServiceWrapper,
  collection: moduleProviders,
  moduleId: 'AppModule',
};

AI Coding Instructions

  • Treat wrapperRef as the authoritative wrapper selected during dependency resolution; use collection when module-level lookup context is needed.
  • Preserve the original token, including string, symbol, class, and custom injection-token values.
  • Keep moduleId associated with the link so cross-module resolution and diagnostics retain module context.
  • Do not assume collection.get(token) always returns the same wrapper as wrapperRef; aliases and lookup rules may affect resolution.

Relationships

  • IMPORTS → InjectionToken
  • IMPORTS → isFunction

Was this page helpful?

Download as PDF
InstanceLink — NestJS head-to-head