Prevent future DI bugs by throwing error

At present we never get optional dependencies, and if that was necessary we'd be explicit about it.
This commit is contained in:
Mattias Granlund 2024-03-18 21:01:29 +01:00
parent debfb48ba7
commit d78f95417d

View File

@ -4,11 +4,15 @@ import type { Readable } from 'svelte/store';
export function getContextByClass<T extends new (...args: any) => InstanceType<T>>( export function getContextByClass<T extends new (...args: any) => InstanceType<T>>(
key: T key: T
): InstanceType<T> { ): InstanceType<T> {
return svelteGetContext<InstanceType<T>>(key); const instance = svelteGetContext<InstanceType<T> | undefined>(key);
if (!instance) throw new Error(`no instance of \`${key.name}\` in context`);
return instance;
} }
export function getContextStoreByClass<T extends new (...args: any) => InstanceType<T>>( export function getContextStoreByClass<T extends new (...args: any) => InstanceType<T>>(
key: T key: T
): Readable<InstanceType<T>> { ): Readable<InstanceType<T>> {
return svelteGetContext<Readable<InstanceType<T>>>(key); const instance = svelteGetContext<Readable<InstanceType<T>> | undefined>(key);
if (!instance) throw new Error(`no instance of \`Readable<${key.name}>\` in context`);
return instance;
} }