Skip to content

Unsafe pluginContext fallback #184

@KaelWD

Description

@KaelWD

#176 with missing properties:

export function createLocaleFallback (): LocaleContext {
return {
size: 0,
t: (key: string) => key,
n: String,
} as unknown as LocaleContext
}

fallback factory exists and we have injection context so the fallback instance is passed to useContext:
function useX<_E extends E = E> (namespace = defaultNamespace): _E {
if (config?.fallback) {
const instance = config.fallback(namespace) as _E
if (!hasInjectionContext()) return instance
return useContext<_E>(namespace, instance)
}

inject returns the fallback so isUndefined is false:
export function useContext<Z> (key: ContextKey<Z>, defaultValue?: Z) {
const context = inject<Z>(key, defaultValue as Z)
if (isUndefined(context)) {
throw new Error(`Context "${String(key)}" not found. Ensure it's provided by an ancestor.`)
}
return context
}

If createLocaleContext is missing we get a runtime error instead of the warning:

const locale = useLocale()
locale.select('de') // Uncaught TypeError: locale.select is not a function

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions