vue-inject-key
provide or inject with a string as the key.
Why: with a string, inject("theme") returns whatever someone provided under that name, typed as whatever you claim it is. Nothing connects the two sides: rename one and the other silently gets undefined, and a library using the same string takes your value. An InjectionKey<T> is a symbol that carries the type: both sides use the same constant, so TypeScript checks the value, and nobody else can collide with it. Guide: Vue.
✕ Rejectedvue-inject-key
ts
import type { Option } from "@evobug_com/safe-ts-std/result";
import { inject, provide } from "vue";
export type Theme = { readonly accent: string };
export function provideTheme(theme: Theme): void {
provide("theme", theme);
}
export function useTheme(): Option<Theme> {
return inject<Theme>("theme");
}✓ Accepted0 diagnostics
ts
import { type InjectionKey, provide } from "vue";
import type { Result } from "@evobug_com/safe-ts-std/result";
import { injectRequired, type MissingInjection } from "@evobug_com/safe-ts-vue/inject";
export type Theme = { readonly accent: string };
export const themeKey: InjectionKey<Theme> = Symbol("theme");
export function provideTheme(theme: Theme): void {
provide(themeKey, theme);
}
export function useTheme(): Result<Theme, MissingInjection> {
return injectRequired(themeKey);
}injectRequired returns an Err saying why when nothing was provided (NotProvided) or when it was called outside setup (OutsideSetup), instead of a bare undefined.
What the rule can't see: a string key held in a variable (provide(THEME, ...) with const THEME = "theme"), and app.provide(...).