Skip to content

Commit 9ac8c66

Browse files
authored
Merge pull request #2891 from zxbodya/ts
improve typings
2 parents baa5fc2 + a9d1a1f commit 9ac8c66

File tree

14 files changed

+209
-91
lines changed

14 files changed

+209
-91
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"main": "index",
77
"files": [
88
"types",
9-
"compiler.js",
9+
"compiler.*",
1010
"register.js",
1111
"index.*",
1212
"internal.*",

src/animate.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
import { cubicOut } from 'svelte/easing';
22
import { is_function } from 'svelte/internal';
33

4-
export function flip(node, animation, params) {
4+
// todo: same as Transition, should it be shared?
5+
export interface AnimationConfig {
6+
delay?: number,
7+
duration?: number,
8+
easing?: (t: number) => number,
9+
css?: (t: number, u: number) => string,
10+
tick?: (t: number, u: number) => void
11+
}
12+
13+
interface FlipParams {
14+
delay: number;
15+
duration: number | ((len: number) => number);
16+
easing: (t: number) => number,
17+
}
18+
19+
export function flip(node: Element, animation: { from: DOMRect, to: DOMRect }, params: FlipParams): AnimationConfig {
520
const style = getComputedStyle(node);
621
const transform = style.transform === 'none' ? '' : style.transform;
722

src/easing.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ Distributed under MIT License https://github.com/mattdesl/eases/blob/master/LICE
55

66
export { identity as linear } from 'svelte/internal';
77

8-
export function backInOut(t) {
8+
export function backInOut(t: number) {
99
const s = 1.70158 * 1.525;
1010
if ((t *= 2) < 1) return 0.5 * (t * t * ((s + 1) * t - s));
1111
return 0.5 * ((t -= 2) * t * ((s + 1) * t + s) + 2);
1212
}
1313

14-
export function backIn(t) {
14+
export function backIn(t: number) {
1515
const s = 1.70158;
1616
return t * t * ((s + 1) * t - s);
1717
}
1818

19-
export function backOut(t) {
19+
export function backOut(t: number) {
2020
const s = 1.70158;
2121
return --t * t * ((s + 1) * t + s) + 1;
2222
}
2323

24-
export function bounceOut(t) {
24+
export function bounceOut(t: number) {
2525
const a = 4.0 / 11.0;
2626
const b = 8.0 / 11.0;
2727
const c = 9.0 / 10.0;
@@ -41,43 +41,43 @@ export function bounceOut(t) {
4141
: 10.8 * t * t - 20.52 * t + 10.72;
4242
}
4343

44-
export function bounceInOut(t) {
44+
export function bounceInOut(t: number) {
4545
return t < 0.5
4646
? 0.5 * (1.0 - bounceOut(1.0 - t * 2.0))
4747
: 0.5 * bounceOut(t * 2.0 - 1.0) + 0.5;
4848
}
4949

50-
export function bounceIn(t) {
50+
export function bounceIn(t: number) {
5151
return 1.0 - bounceOut(1.0 - t);
5252
}
5353

54-
export function circInOut(t) {
54+
export function circInOut(t: number) {
5555
if ((t *= 2) < 1) return -0.5 * (Math.sqrt(1 - t * t) - 1);
5656
return 0.5 * (Math.sqrt(1 - (t -= 2) * t) + 1);
5757
}
5858

59-
export function circIn(t) {
59+
export function circIn(t: number) {
6060
return 1.0 - Math.sqrt(1.0 - t * t);
6161
}
6262

63-
export function circOut(t) {
63+
export function circOut(t: number) {
6464
return Math.sqrt(1 - --t * t);
6565
}
6666

67-
export function cubicInOut(t) {
67+
export function cubicInOut(t: number) {
6868
return t < 0.5 ? 4.0 * t * t * t : 0.5 * Math.pow(2.0 * t - 2.0, 3.0) + 1.0;
6969
}
7070

71-
export function cubicIn(t) {
71+
export function cubicIn(t: number) {
7272
return t * t * t;
7373
}
7474

75-
export function cubicOut(t) {
75+
export function cubicOut(t: number) {
7676
const f = t - 1.0;
7777
return f * f * f + 1.0;
7878
}
7979

80-
export function elasticInOut(t) {
80+
export function elasticInOut(t: number) {
8181
return t < 0.5
8282
? 0.5 *
8383
Math.sin(((+13.0 * Math.PI) / 2) * 2.0 * t) *
@@ -88,84 +88,84 @@ export function elasticInOut(t) {
8888
1.0;
8989
}
9090

91-
export function elasticIn(t) {
91+
export function elasticIn(t: number) {
9292
return Math.sin((13.0 * t * Math.PI) / 2) * Math.pow(2.0, 10.0 * (t - 1.0));
9393
}
9494

95-
export function elasticOut(t) {
95+
export function elasticOut(t: number) {
9696
return (
9797
Math.sin((-13.0 * (t + 1.0) * Math.PI) / 2) * Math.pow(2.0, -10.0 * t) + 1.0
9898
);
9999
}
100100

101-
export function expoInOut(t) {
101+
export function expoInOut(t: number) {
102102
return t === 0.0 || t === 1.0
103103
? t
104104
: t < 0.5
105105
? +0.5 * Math.pow(2.0, 20.0 * t - 10.0)
106106
: -0.5 * Math.pow(2.0, 10.0 - t * 20.0) + 1.0;
107107
}
108108

109-
export function expoIn(t) {
109+
export function expoIn(t: number) {
110110
return t === 0.0 ? t : Math.pow(2.0, 10.0 * (t - 1.0));
111111
}
112112

113-
export function expoOut(t) {
113+
export function expoOut(t: number) {
114114
return t === 1.0 ? t : 1.0 - Math.pow(2.0, -10.0 * t);
115115
}
116116

117-
export function quadInOut(t) {
117+
export function quadInOut(t: number) {
118118
t /= 0.5;
119119
if (t < 1) return 0.5 * t * t;
120120
t--;
121121
return -0.5 * (t * (t - 2) - 1);
122122
}
123123

124-
export function quadIn(t) {
124+
export function quadIn(t: number) {
125125
return t * t;
126126
}
127127

128-
export function quadOut(t) {
128+
export function quadOut(t: number) {
129129
return -t * (t - 2.0);
130130
}
131131

132-
export function quartInOut(t) {
132+
export function quartInOut(t: number) {
133133
return t < 0.5
134134
? +8.0 * Math.pow(t, 4.0)
135135
: -8.0 * Math.pow(t - 1.0, 4.0) + 1.0;
136136
}
137137

138-
export function quartIn(t) {
138+
export function quartIn(t: number) {
139139
return Math.pow(t, 4.0);
140140
}
141141

142-
export function quartOut(t) {
142+
export function quartOut(t: number) {
143143
return Math.pow(t - 1.0, 3.0) * (1.0 - t) + 1.0;
144144
}
145145

146-
export function quintInOut(t) {
146+
export function quintInOut(t: number) {
147147
if ((t *= 2) < 1) return 0.5 * t * t * t * t * t;
148148
return 0.5 * ((t -= 2) * t * t * t * t + 2);
149149
}
150150

151-
export function quintIn(t) {
151+
export function quintIn(t: number) {
152152
return t * t * t * t * t;
153153
}
154154

155-
export function quintOut(t) {
155+
export function quintOut(t: number) {
156156
return --t * t * t * t * t + 1;
157157
}
158158

159-
export function sineInOut(t) {
159+
export function sineInOut(t: number) {
160160
return -0.5 * (Math.cos(Math.PI * t) - 1);
161161
}
162162

163-
export function sineIn(t) {
163+
export function sineIn(t: number) {
164164
const v = Math.cos(t * Math.PI * 0.5);
165165
if (Math.abs(v) < 1e-14) return 1;
166166
else return 1 - v;
167167
}
168168

169-
export function sineOut(t) {
169+
export function sineOut(t: number) {
170170
return Math.sin((t * Math.PI) / 2);
171171
}

src/internal/animations.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import { identity as linear, noop, now } from './utils';
22
import { loop } from './loop';
33
import { create_rule, delete_rule } from './style_manager';
4+
import { AnimationConfig } from '../animate';
45

5-
export function create_animation(node, from, fn, params) {
6+
7+
//todo: documentation says it is DOMRect, but in IE it would be ClientRect
8+
type PositionRect = DOMRect|ClientRect;
9+
10+
type AnimationFn = (node: Element, { from, to }: { from: PositionRect, to: PositionRect }, params: any) => AnimationConfig;
11+
12+
export function create_animation(node: Element & ElementCSSInlineStyle, from: PositionRect, fn: AnimationFn, params) {
613
if (!from) return noop;
714

815
const to = node.getBoundingClientRect();
916
if (from.left === to.left && from.right === to.right && from.top === to.top && from.bottom === to.bottom) return noop;
1017

18+
1119
const {
1220
delay = 0,
1321
duration = 300,
1422
easing = linear,
23+
// @ts-ignore todo: should this be separated from destructuring? Or start/end added to public api and documentation?
1524
start: start_time = now() + delay,
25+
// @ts-ignore todo:
1626
end = start_time + duration,
1727
tick = noop,
1828
css
@@ -67,7 +77,7 @@ export function create_animation(node, from, fn, params) {
6777
return stop;
6878
}
6979

70-
export function fix_position(node) {
80+
export function fix_position(node: Element & ElementCSSInlineStyle) {
7181
const style = getComputedStyle(node);
7282

7383
if (style.position !== 'absolute' && style.position !== 'fixed') {

src/internal/dom.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ export function object_without_properties<T,K extends keyof T>(obj:T, exclude: K
5353
return target;
5454
}
5555

56-
export function svg_element(name:string):SVGElement {
57-
return document.createElementNS('http://www.w3.org/2000/svg', name);
56+
export function svg_element<K extends keyof SVGElementTagNameMap>(name:K):SVGElement {
57+
return document.createElementNS<K>('http://www.w3.org/2000/svg', name);
5858
}
5959

6060
export function text(data:string) {
@@ -95,7 +95,7 @@ export function attr(node: Element, attribute: string, value?: string) {
9595
else node.setAttribute(attribute, value);
9696
}
9797

98-
export function set_attributes(node: HTMLElement, attributes: { [x: string]: string; }) {
98+
export function set_attributes(node: Element & ElementCSSInlineStyle, attributes: { [x: string]: string; }) {
9999
for (const key in attributes) {
100100
if (key === 'style') {
101101
node.style.cssText = attributes[key];

src/internal/loop.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { now, raf } from './utils';
22

3-
export interface Task { abort(): void; promise: Promise<undefined> }
3+
export interface Task { abort(): void; promise: Promise<void> }
44

55
const tasks = new Set();
66
let running = false;
@@ -32,7 +32,7 @@ export function loop(fn: (number)=>void): Task {
3232
}
3333

3434
return {
35-
promise: new Promise<undefined>(fulfil => {
35+
promise: new Promise<void>(fulfil => {
3636
tasks.add(task = [fn, fulfil]);
3737
}),
3838
abort() {

src/internal/ssr.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { set_current_component, current_component } from './lifecycle';
22
import { run_all, blank_object } from './utils';
3+
import { Readable } from 'svelte/store';
34

45
export const invalid_attribute_name_character = /[\s'">/=\u{FDD0}-\u{FDEF}\u{FFFE}\u{FFFF}\u{1FFFE}\u{1FFFF}\u{2FFFE}\u{2FFFF}\u{3FFFE}\u{3FFFF}\u{4FFFE}\u{4FFFF}\u{5FFFE}\u{5FFFF}\u{6FFFE}\u{6FFFF}\u{7FFFE}\u{7FFFF}\u{8FFFE}\u{8FFFF}\u{9FFFE}\u{9FFFF}\u{AFFFE}\u{AFFFF}\u{BFFFE}\u{BFFFF}\u{CFFFE}\u{CFFFF}\u{DFFFE}\u{DFFFF}\u{EFFFE}\u{EFFFF}\u{FFFFE}\u{FFFFF}\u{10FFFE}\u{10FFFF}]/u;
56
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
@@ -113,7 +114,7 @@ export function create_ssr_component(fn) {
113114
};
114115
}
115116

116-
export function get_store_value(store) {
117+
export function get_store_value<T>(store: Readable<T>): T | undefined {
117118
let value;
118119
store.subscribe(_ => value = _)();
119120
return value;

src/internal/style_manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ let active = 0;
66
let current_rules = {};
77

88
// https://github.com/darkskyapp/string-hash/blob/master/index.js
9-
function hash(str) {
9+
function hash(str: string) {
1010
let hash = 5381;
1111
let i = str.length;
1212

1313
while (i--) hash = ((hash << 5) - hash) ^ str.charCodeAt(i);
1414
return hash >>> 0;
1515
}
1616

17-
export function create_rule(node, a, b, duration, delay, ease, fn, uid = 0) {
17+
export function create_rule(node: Element & ElementCSSInlineStyle, a: number, b: number, duration: number, delay: number, ease: (t: number) => number, fn: (t: number, u: number) => string, uid: number = 0) {
1818
const step = 16.666 / duration;
1919
let keyframes = '{\n';
2020

@@ -44,7 +44,7 @@ export function create_rule(node, a, b, duration, delay, ease, fn, uid = 0) {
4444
return name;
4545
}
4646

47-
export function delete_rule(node, name?) {
47+
export function delete_rule(node: Element & ElementCSSInlineStyle, name?: string) {
4848
node.style.animation = (node.style.animation || '')
4949
.split(', ')
5050
.filter(name

0 commit comments

Comments
 (0)