-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide ContextKey type for better typing of `setContext/getCon…
…text` 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 #8941
- Loading branch information
1 parent
d85d5a0
commit f076646
Showing
7 changed files
with
91 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"svelte": patch | ||
--- | ||
|
||
feat: provide ContextKey type for better typing of `setContext/getContext` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters