1
- import { Readable } from 'svelte/store' ;
1
+ import { Readable , Subscriber , Invalidator , Writable } from 'svelte/store' ;
2
+ import { SvelteComponent } from '..' ;
2
3
3
- export function noop ( ) { }
4
+ export function noop ( ) { }
4
5
5
- export const identity = x => x ;
6
+ export function identity < T > ( x : T ) : T {
7
+ return x ;
8
+ }
6
9
7
- export function assign < T , S > ( tar : T , src : S ) : T & S {
10
+ export function assign < T , S > ( target : T , source : S ) : T & S {
8
11
// @ts -ignore
9
- for ( const k in src ) tar [ k ] = src [ k ] ;
10
- return tar as T & S ;
12
+ for ( const k in source ) target [ k ] = source [ k ] ;
13
+ return target as T & S ;
11
14
}
12
15
13
16
export function is_promise < T = any > ( value : any ) : value is PromiseLike < T > {
@@ -20,82 +23,83 @@ export function add_location(element, file, line, column, char) {
20
23
} ;
21
24
}
22
25
23
- export function run ( fn ) {
24
- return fn ( ) ;
26
+ export function blank_object ( ) : { } {
27
+ return Object . create ( null ) ;
25
28
}
26
29
27
- export function blank_object ( ) {
28
- return Object . create ( null ) ;
30
+ type FunctionWithoutArguments = ( ) => unknown
31
+
32
+ export function run ( fn : FunctionWithoutArguments ) {
33
+ return fn ( ) ;
29
34
}
30
35
31
- export function run_all ( fns : Function [ ] ) {
36
+ export function run_all ( fns : FunctionWithoutArguments [ ] ) {
32
37
fns . forEach ( run ) ;
33
38
}
34
39
35
40
export function is_function ( thing : any ) : thing is Function {
36
41
return typeof thing === 'function' ;
37
42
}
38
43
39
- export function safe_not_equal ( a , b ) {
40
- return a != a ? b == b : a !== b || ( ( a && typeof a === 'object' ) || typeof a === 'function' ) ;
41
- }
42
-
43
- let src_url_equal_anchor ;
44
+ let src_url_equal_anchor : HTMLAnchorElement ;
44
45
45
- export function src_url_equal ( element_src , url ) {
46
+ export function src_url_equal ( element_src : string , url : string ) {
46
47
if ( ! src_url_equal_anchor ) {
47
48
src_url_equal_anchor = document . createElement ( 'a' ) ;
48
49
}
49
50
src_url_equal_anchor . href = url ;
50
51
return element_src === src_url_equal_anchor . href ;
51
52
}
52
53
53
- export function not_equal ( a , b ) {
54
+ export function safe_not_equal ( a : unknown , b : unknown ) {
55
+ return a != a ? b == b : a !== b || ( ( a && typeof a === 'object' ) || typeof a === 'function' ) ;
56
+ }
57
+
58
+ export function not_equal ( a : unknown , b : unknown ) {
54
59
return a != a ? b == b : a !== b ;
55
60
}
56
61
57
- export function is_empty ( obj ) {
62
+ export function is_empty ( obj : Record < string | number | symbol , unknown > ) {
58
63
return Object . keys ( obj ) . length === 0 ;
59
64
}
60
65
61
- export function validate_store ( store , name ) {
66
+ export function validate_store < S extends Readable < unknown > > ( store : S , name : string ) {
62
67
if ( store != null && typeof store . subscribe !== 'function' ) {
63
68
throw new Error ( `'${ name } ' is not a store with a 'subscribe' method` ) ;
64
69
}
65
70
}
66
71
67
- export function subscribe ( store , ... callbacks ) {
72
+ export function subscribe < T , S extends Readable < T > > ( store : S , run : Subscriber < T > , invalidate ?: Invalidator < T > ) {
68
73
if ( store == null ) {
69
74
return noop ;
70
75
}
71
- const unsub = store . subscribe ( ...callbacks ) ;
72
- return unsub . unsubscribe ? ( ) => unsub . unsubscribe ( ) : unsub ;
76
+ return store . subscribe ( run , invalidate ) ;
73
77
}
74
78
75
- export function get_store_value < T > ( store : Readable < T > ) : T {
76
- let value ;
77
- subscribe ( store , _ => value = _ ) ( ) ;
79
+ export function get_store_value < T , S extends Readable < T > > ( store : S ) : T {
80
+ let value : T ;
81
+ subscribe < T , S > ( store , v => value = v ) ( ) ;
78
82
return value ;
79
83
}
80
84
81
- export function component_subscribe ( component , store , callback ) {
85
+ export function component_subscribe < T , S extends Readable < T > > ( component : SvelteComponent , store : S , callback : Subscriber < T > ) {
82
86
component . $$ . on_destroy . push ( subscribe ( store , callback ) ) ;
83
87
}
84
88
85
- export function create_slot ( definition , ctx , $$scope , fn ) {
89
+ export function create_slot ( definition , ctx , $$scope , fn : Function ) {
86
90
if ( definition ) {
87
91
const slot_ctx = get_slot_context ( definition , ctx , $$scope , fn ) ;
88
92
return definition [ 0 ] ( slot_ctx ) ;
89
93
}
90
94
}
91
95
92
- function get_slot_context ( definition , ctx , $$scope , fn ) {
96
+ function get_slot_context ( definition , ctx , $$scope , fn : Function ) {
93
97
return definition [ 1 ] && fn
94
98
? assign ( $$scope . ctx . slice ( ) , definition [ 1 ] ( fn ( ctx ) ) )
95
99
: $$scope . ctx ;
96
100
}
97
101
98
- export function get_slot_changes ( definition , $$scope , dirty , fn ) {
102
+ export function get_slot_changes ( definition , $$scope , dirty , fn : Function ) {
99
103
if ( definition [ 2 ] && fn ) {
100
104
const lets = definition [ 2 ] ( fn ( dirty ) ) ;
101
105
@@ -164,25 +168,27 @@ export function compute_slots(slots) {
164
168
return result ;
165
169
}
166
170
167
- export function once ( fn ) {
171
+ export function once ( fn : Function ) {
168
172
let ran = false ;
169
- return function ( this : any , ...args ) {
173
+ return function ( this : any , ...args : unknown [ ] ) {
170
174
if ( ran ) return ;
171
175
ran = true ;
172
176
fn . call ( this , ...args ) ;
173
177
} ;
174
178
}
175
179
176
- export function null_to_empty ( value ) {
177
- return value == null ? '' : value ;
180
+ export function null_to_empty < T > ( value : T ) : T extends null | undefined ? '' : T {
181
+ return ( value == null ? '' : value ) as T extends null | undefined ? '' : T ;
178
182
}
179
183
180
- export function set_store_value ( store , ret , value ) {
184
+ export function set_store_value < T , S extends Writable < T > > ( store : S , ret : Node , value : T ) {
181
185
store . set ( value ) ;
182
186
return ret ;
183
187
}
184
188
185
- export const has_prop = ( obj , prop ) => Object . prototype . hasOwnProperty . call ( obj , prop ) ;
189
+ export function has_prop < X extends { } , Y extends PropertyKey > ( obj : X , prop : Y ) : obj is X & Record < Y , unknown > {
190
+ return Object . prototype . hasOwnProperty . call ( obj , prop ) ;
191
+ }
186
192
187
193
export function action_destroyer ( action_result ) {
188
194
return action_result && is_function ( action_result . destroy ) ? action_result . destroy : noop ;
0 commit comments