--- title: Use Symbol Keys for Provide/Inject in Large Applications impact: MEDIUM impactDescription: String injection keys can collide in large applications or when using third-party components type: best-practice tags: [vue3, provide-inject, typescript, architecture, component-library] --- # Use Symbol Keys for Provide/Inject in Large Applications **Impact: MEDIUM** - Using string keys for provide/inject works for small applications but can cause key collisions in large apps, component libraries, or when multiple teams work on the same codebase. Symbol keys guarantee uniqueness and provide better TypeScript integration. ## Task Checklist - [ ] Use Symbol keys for provide/inject in large applications - [ ] Export symbols from a dedicated keys file - [ ] Use `InjectionKey` for TypeScript type safety - [ ] Reserve string keys for simple, local use cases only ## The Problem: String Key Collisions **Risky - String keys can collide:** ```vue ``` ## Solution: Symbol Keys **Correct - Unique symbols prevent collisions:** ```js // injection-keys.js export const ThemeKey = Symbol('theme') export const UserKey = Symbol('user') export const ConfigKey = Symbol('config') ``` ```vue ``` ```vue ``` ## TypeScript: InjectionKey for Type Safety Vue provides `InjectionKey` for strongly-typed injection: ```ts // injection-keys.ts import type { InjectionKey, Ref } from 'vue' // Define the injected type interface User { id: string name: string email: string } interface ThemeConfig { mode: 'light' | 'dark' primaryColor: string } // Create typed injection keys export const UserKey: InjectionKey> = Symbol('user') export const ThemeKey: InjectionKey> = Symbol('theme') export const LoggerKey: InjectionKey<(msg: string) => void> = Symbol('logger') ``` ```vue ``` ```vue ``` ## Pattern: Injection Keys File Organization For larger applications, organize keys by feature: ``` src/ injection-keys/ index.ts # Re-exports all keys auth.ts # Auth-related keys theme.ts # Theme-related keys feature-x.ts # Feature-specific keys ``` ```ts // injection-keys/auth.ts import type { InjectionKey, Ref, ComputedRef } from 'vue' export interface AuthState { user: User | null isAuthenticated: boolean permissions: string[] } export interface AuthActions { login: (credentials: Credentials) => Promise logout: () => Promise checkPermission: (permission: string) => boolean } export const AuthStateKey: InjectionKey> = Symbol('auth-state') export const AuthActionsKey: InjectionKey = Symbol('auth-actions') ``` ```ts // injection-keys/index.ts export * from './auth' export * from './theme' export * from './feature-x' ``` ## Handling Missing Injections with Types TypeScript helps catch missing providers: ```vue ``` ## When String Keys Are Still OK String keys are acceptable for: - Small applications with few providers - Local component trees with clear boundaries - Quick prototypes - App-level provides with unique, namespaced strings ```vue ``` ## Reference - [Vue.js Provide/Inject - Working with Symbol Keys](https://vuejs.org/guide/components/provide-inject.html#working-with-symbol-keys) - [Vue.js TypeScript - Typing Provide/Inject](https://vuejs.org/guide/typescript/composition-api.html#typing-provide-inject)