mirror of https://github.com/sveltejs/svelte
Not completly ideal because you can circumvent the type safety by doing `getContext<SomeType>(context_key)` - changing this would require a breaking change, which we could do in Svelte 6 after we've given `ContextKey` some time to establish itself. Also doesn't add the interesting type narrowing idea in https://github.com/KamenKolev/svelte-typed-context/blob/master/index.ts#L14 (yet), probably easier to do together with said breaking change. closes #8941pull/11042/head
parent
d85d5a060b
commit
f076646214
@ -0,0 +1,5 @@
|
||||
---
|
||||
"svelte": patch
|
||||
---
|
||||
|
||||
feat: provide ContextKey type for better typing of `setContext/getContext`
|
@ -1,2 +1,5 @@
|
||||
/** Anything except a function */
|
||||
export type NotFunction<T> = T extends Function ? never : T;
|
||||
|
||||
/** Helper function to detect `any` */
|
||||
export type IsAny<T> = 0 extends 1 & T ? true : false;
|
||||
|
@ -0,0 +1,39 @@
|
||||
import { getContext, setContext, type ContextKey } from 'svelte';
|
||||
|
||||
const context_key: ContextKey<boolean> = Symbol('foo');
|
||||
// @ts-expect-error
|
||||
const context_key_wrong: ContextKey<boolean> = true;
|
||||
|
||||
setContext(context_key, true);
|
||||
// @ts-expect-error
|
||||
setContext(context_key, '');
|
||||
|
||||
const ok_1: boolean | undefined = getContext(context_key);
|
||||
const sadly_ok: string = getContext<string>(context_key); // making this an error at some point would be good; requires a breaking change
|
||||
// @ts-expect-error
|
||||
const not_ok_1: boolean = getContext(context_key);
|
||||
// @ts-expect-error
|
||||
const not_ok_2: string = getContext(context_key);
|
||||
|
||||
const any_key: any = {};
|
||||
|
||||
setContext(any_key, true);
|
||||
|
||||
const ok_2: boolean = getContext(any_key);
|
||||
const ok_3: string = getContext(any_key);
|
||||
const ok_4: string = getContext<string>(any_key);
|
||||
// @ts-expect-error
|
||||
const not_ok_3: string = getContext<boolean>(any_key);
|
||||
|
||||
const boolean_key = true;
|
||||
|
||||
setContext(boolean_key, true);
|
||||
setContext<boolean>(boolean_key, true);
|
||||
// @ts-expect-error
|
||||
setContext<boolean>(boolean_key, '');
|
||||
|
||||
const ok_5: boolean = getContext(boolean_key);
|
||||
const ok_6: string = getContext(boolean_key);
|
||||
const ok_7: string = getContext<string>(boolean_key);
|
||||
// @ts-expect-error
|
||||
const not_ok_4: string = getContext<boolean>(boolean_key);
|
Loading…
Reference in new issue