Skip to content

Commit 49b869b

Browse files
committed
add some types to utils.ts
1 parent 281ee3f commit 49b869b

File tree

3 files changed

+44
-37
lines changed

3 files changed

+44
-37
lines changed

src/compiler/compile/render_dom/invalidate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export function invalidate(renderer: Renderer, scope: Scope, node: Node, names:
5050
const extra_args = tail.map(variable => get_invalidated(variable)).filter(Boolean);
5151

5252
if (is_store_value) {
53+
// TODO: check why there are 4 parameters, but `set_store_value` only expects 3
5354
return x`@set_store_value(${head.name.slice(1)}, ${node}, ${head.name}, ${extra_args})`;
5455
}
5556

src/runtime/internal/utils.ts

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import { Readable } from 'svelte/store';
1+
import { Readable, Subscriber, Invalidator, Writable } from 'svelte/store';
2+
import { SvelteComponent } from '..';
23

3-
export function noop() {}
4+
export function noop() { }
45

5-
export const identity = x => x;
6+
export function identity<T>(x: T): T {
7+
return x;
8+
}
69

7-
export function assign<T, S>(tar: T, src: S): T & S {
10+
export function assign<T, S>(target: T, source: S): T & S {
811
// @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;
1114
}
1215

1316
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) {
2023
};
2124
}
2225

23-
export function run(fn) {
24-
return fn();
26+
export function blank_object(): {} {
27+
return Object.create(null);
2528
}
2629

27-
export function blank_object() {
28-
return Object.create(null);
30+
type FunctionWithoutArguments = () => unknown
31+
32+
export function run(fn: FunctionWithoutArguments) {
33+
return fn();
2934
}
3035

31-
export function run_all(fns: Function[]) {
36+
export function run_all(fns: FunctionWithoutArguments[]) {
3237
fns.forEach(run);
3338
}
3439

3540
export function is_function(thing: any): thing is Function {
3641
return typeof thing === 'function';
3742
}
3843

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;
4445

45-
export function src_url_equal(element_src, url) {
46+
export function src_url_equal(element_src: string, url: string) {
4647
if (!src_url_equal_anchor) {
4748
src_url_equal_anchor = document.createElement('a');
4849
}
4950
src_url_equal_anchor.href = url;
5051
return element_src === src_url_equal_anchor.href;
5152
}
5253

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) {
5459
return a != a ? b == b : a !== b;
5560
}
5661

57-
export function is_empty(obj) {
62+
export function is_empty(obj: Record<string | number | symbol, unknown>) {
5863
return Object.keys(obj).length === 0;
5964
}
6065

61-
export function validate_store(store, name) {
66+
export function validate_store<S extends Readable<unknown>>(store: S, name: string) {
6267
if (store != null && typeof store.subscribe !== 'function') {
6368
throw new Error(`'${name}' is not a store with a 'subscribe' method`);
6469
}
6570
}
6671

67-
export function subscribe(store, ...callbacks) {
72+
export function subscribe<T, S extends Readable<T>>(store: S, run: Subscriber<T>, invalidate?: Invalidator<T>) {
6873
if (store == null) {
6974
return noop;
7075
}
71-
const unsub = store.subscribe(...callbacks);
72-
return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub;
76+
return store.subscribe(run, invalidate);
7377
}
7478

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)();
7882
return value;
7983
}
8084

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>) {
8286
component.$$.on_destroy.push(subscribe(store, callback));
8387
}
8488

85-
export function create_slot(definition, ctx, $$scope, fn) {
89+
export function create_slot(definition, ctx, $$scope, fn: Function) {
8690
if (definition) {
8791
const slot_ctx = get_slot_context(definition, ctx, $$scope, fn);
8892
return definition[0](slot_ctx);
8993
}
9094
}
9195

92-
function get_slot_context(definition, ctx, $$scope, fn) {
96+
function get_slot_context(definition, ctx, $$scope, fn: Function) {
9397
return definition[1] && fn
9498
? assign($$scope.ctx.slice(), definition[1](fn(ctx)))
9599
: $$scope.ctx;
96100
}
97101

98-
export function get_slot_changes(definition, $$scope, dirty, fn) {
102+
export function get_slot_changes(definition, $$scope, dirty, fn: Function) {
99103
if (definition[2] && fn) {
100104
const lets = definition[2](fn(dirty));
101105

@@ -164,25 +168,27 @@ export function compute_slots(slots) {
164168
return result;
165169
}
166170

167-
export function once(fn) {
171+
export function once(fn: Function) {
168172
let ran = false;
169-
return function(this: any, ...args) {
173+
return function (this: any, ...args: unknown[]) {
170174
if (ran) return;
171175
ran = true;
172176
fn.call(this, ...args);
173177
};
174178
}
175179

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;
178182
}
179183

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) {
181185
store.set(value);
182186
return ret;
183187
}
184188

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+
}
186192

187193
export function action_destroyer(action_result) {
188194
return action_result && is_function(action_result.destroy) ? action_result.destroy : noop;

src/runtime/store/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type Unsubscriber = () => void;
1010
export type Updater<T> = (value: T) => T;
1111

1212
/** Cleanup logic callback. */
13-
type Invalidator<T> = (value?: T) => void;
13+
export type Invalidator<T> = (value?: T) => void;
1414

1515
/** Start and stop notification callbacks. */
1616
export type StartStopNotifier<T> = (set: Subscriber<T>) => Unsubscriber | void;

0 commit comments

Comments
 (0)