File tree Expand file tree Collapse file tree 4 files changed +24
-16
lines changed 
documentation/docs/06-runtime Expand file tree Collapse file tree 4 files changed +24
-16
lines changed Original file line number Diff line number Diff line change @@ -83,27 +83,18 @@ Svelte will warn you if you get it wrong.
8383
8484## Type-safe context  
8585
86- A useful pattern is  to wrap the calls to  ` setContext `  and ` getContext `  inside helper functions that let you preserve  type safety:
86+ As an alternative  to using  ` setContext `  and ` getContext `  directly, you can use them via  ` createContext ` . This gives you  type safety and makes it unnecessary to use a key :
8787
88- ``` js 
89- // / file: context.js 
88+ ``` ts 
89+ // / file: context.ts 
9090//  @filename: ambient.d.ts
9191interface  User  {}
9292
93- //  @filename: index.js 
93+ //  @filename: index.ts 
9494//  ---cut---
95- import  { getContext , setContext  } from  ' svelte' 
96- 
97- const  key  =  {};
98- 
99- /**  @param  {User}  user  */ 
100- export  function  setUserContext (user ) {
101- 	setContext (key, user);
102- }
95+ import  { createContext  } from  ' svelte' 
10396
104- export  function  getUserContext () {
105- 	return  /**  @type  {User}  */ getContext (key));
106- }
97+ export const =  createContext <User >();
10798``` 
10899
109100## Replacing global state  
Original file line number Diff line number Diff line change @@ -71,8 +71,12 @@ export function set_dev_current_component_function(fn) {
7171
7272/** 
7373 * Returns a `[get, set]` pair of functions for working with context in a type-safe way. 
74+  * 
75+  * `get` will throw an error if no parent component called `set`. 
76+  * 
7477 * @template  T 
7578 * @returns  {[() => T, (context: T) => T] } 
79+  * @since  5.40.0 
7680 */ 
7781export  function  createContext ( )  { 
7882	const  key  =  { } ; 
@@ -93,6 +97,8 @@ export function createContext() {
9397 * Retrieves the context that belongs to the closest parent component with the specified `key`. 
9498 * Must be called during component initialisation. 
9599 * 
100+  * [`createContext`](https://svelte.dev/docs/svelte/svelte#createContext) is a type-safe alternative. 
101+  * 
96102 * @template  T 
97103 * @param  {any } key 
98104 * @returns  {T } 
@@ -110,6 +116,8 @@ export function getContext(key) {
110116 * 
111117 * Like lifecycle functions, this must be called during component initialisation. 
112118 * 
119+  * [`createContext`](https://svelte.dev/docs/svelte/svelte#createContext) is a type-safe alternative. 
120+  * 
113121 * @template  T 
114122 * @param  {any } key 
115123 * @param  {T } context 
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ export function set_ssr_context(v) {
1313/** 
1414 * @template  T 
1515 * @returns  {[() => T, (context: T) => T] } 
16+  * @since  5.40.0 
1617 */ 
1718export  function  createContext ( )  { 
1819	const  key  =  { } ; 
Original file line number Diff line number Diff line change @@ -450,12 +450,18 @@ declare module 'svelte' {
450450	type  NotFunction < T >  =  T  extends  Function  ? never  : T ; 
451451	/** 
452452	 * Returns a `[get, set]` pair of functions for working with context in a type-safe way. 
453- 	 * */ 
453+ 	 * 
454+ 	 * `get` will throw an error if no parent component called `set`. 
455+ 	 * 
456+ 	 * @since  5.40.0 
457+ 	 */ 
454458	export  function  createContext < T > ( ) : [ ( )  =>  T ,  ( context : T )  =>  T ] ; 
455459	/** 
456460	 * Retrieves the context that belongs to the closest parent component with the specified `key`. 
457461	 * Must be called during component initialisation. 
458462	 * 
463+ 	 * [`createContext`](https://svelte.dev/docs/svelte/svelte#createContext) is a type-safe alternative. 
464+ 	 * 
459465	 * */ 
460466	export  function  getContext < T > ( key : any ) : T ; 
461467	/** 
@@ -465,6 +471,8 @@ declare module 'svelte' {
465471	 * 
466472	 * Like lifecycle functions, this must be called during component initialisation. 
467473	 * 
474+ 	 * [`createContext`](https://svelte.dev/docs/svelte/svelte#createContext) is a type-safe alternative. 
475+ 	 * 
468476	 * */ 
469477	export  function  setContext < T > ( key : any ,  context : T ) : T ; 
470478	/** 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments