Skip to content

Support Symbol as Dependency Identifier #46

@l246804

Description

@l246804

Is your feature request related to a problem? Please describe

I'm developing an SDK library that needs to provide built-in classes, but I want to use Symbol variables as dependency identifiers when registering them externally. Currently, the library only supports string names for dependencies, which can lead to naming conflicts with other modules that might use the same string identifiers.

Describe the solution you'd like

I'd like the dependency injection/system to support Symbol variables as valid dependency names/identifiers. Symbols are guaranteed to be unique and would perfectly solve the naming collision problem in modular applications.

For example:

// refer vue3 InjectionKey
interface ModuleConstraint<T> {}

type ModuleKey<T> = symbol & ModuleConstraint<T>

class Router {
  toggle() {
    // ...
  }
}

const ROUTER_ID: ModuleKey<Router> = Symbol('router')

const sdk = new Injector([
  {
    [ROUTER_ID]: ['type', Router]
  }
])

// a typed instance of a component
const router = sdk.get(ROUTER_ID)
router.toggle()

Describe alternatives you've considered

  1. Using UUIDs or long random strings as names - but this is less elegant and still has (extremely small) potential for conflicts

  2. Using namespaced strings (e.g., 'my-sdk:dependency-name') - better but still not as foolproof as Symbols

  3. Maintaining a separate dependency container just for my SDK - which would complicate usage for end users

Additional context

This is particularly important for SDKs/libraries that need to coexist in applications with other third-party code. Symbols would provide a clean, conflict-free way to identify dependencies. Many modern JavaScript APIs (like Vue 3's injection system) already support Symbols for this purpose.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions