add more types

pull/7324/head
Ivan Hofer 4 years ago
parent d8f28f8612
commit 5f232ae123

@ -1,4 +1,5 @@
import { has_prop } from './utils';
import { Fragment } from './types.js';
// marks a part in the code where types
// 1. should be improved or
@ -228,23 +229,6 @@ export function detach(node: Node) {
}
}
// TODO: import this interface from /src/runtime/internal/Component.ts
interface Fragment {
key: string | null;
first: null;
/* create */ c: () => void;
/* claim */ l: (nodes: any) => void;
/* hydrate */ h: () => void;
/* mount */ m: (target: HTMLElement, anchor: any) => void;
/* update */ p: (ctx: any, dirty: any) => void;
/* measure */ r: () => void;
/* fix */ f: () => void;
/* animate */ a: () => void;
/* intro */ i: (local: any) => void;
/* outro */ o: (local: any) => void;
/* destroy */ d: (detaching: 0 | 1) => void;
}
export function destroy_each(iterations: Fragment[], detaching: 0 | 1) {
for (let i = 0; i < iterations.length; i += 1) {
if (iterations[i]) iterations[i].d(detaching);
@ -274,7 +258,7 @@ export function object_without_properties<T, K extends keyof T>(obj: T, exclude:
return target;
}
export function svg_element<K extends keyof SVGElementTagNameMap>(name: K): SVGElement {
export function svg_element<K extends keyof SVGElementTagNameMap>(name: K): SVGElementTagNameMap[K] {
return document.createElementNS<K>('http://www.w3.org/2000/svg', name);
}
@ -296,27 +280,27 @@ export function listen(node: EventTarget, event: string, handler: EventListenerO
}
export function prevent_default<E extends Event>(fn: (event: E) => void) {
return function (this: unknown, event: E) {
return function(this: unknown, event: E) {
event.preventDefault();
return fn.call(this, event);
};
}
export function stop_propagation<E extends Event>(fn: (event: E) => void) {
return function (this: unknown, event: E) {
return function(this: unknown, event: E) {
event.stopPropagation();
return fn.call(this, event);
};
}
export function self<E extends Event>(fn: (event: E) => void) {
return function (this: unknown, event: E) {
return function(this: unknown, event: E) {
if (event.target === this) fn.call(this, event);
};
}
export function trusted<E extends Event>(fn: (event: E) => void) {
return function (this: unknown, event: E) {
return function(this: unknown, event: E) {
if (event.isTrusted) fn.call(this, event);
};
}
@ -358,7 +342,6 @@ export function set_custom_element_data_map(node: Element, data_map: Record<stri
export function set_custom_element_data(node: Element, prop: string, value: string) {
if (prop in node) {
//@ts-ignore
node[prop] = typeof node[(prop as keyof typeof node)] === 'boolean' && value === '' ? true : value;
} else {
attr(node, prop, value);
@ -380,7 +363,7 @@ export function get_binding_group_value(group: HTMLInputElementEx[], __value: st
return Array.from(value);
}
export function to_number(value: TODO) {
export function to_number(value: string | number) {
return value === '' ? null : +value;
}
@ -476,10 +459,29 @@ function claim_node<R extends ChildNodeEx>(nodes: ChildNodeArray, predicate: (no
return resultNode;
}
function claim_element_base(nodes: ChildNodeArray, name: string, attributes: { [key: string]: boolean }, create_element: (name: string) => Element | SVGElement) {
return claim_node<Element | SVGElement>(
function claim_element_base<Map extends HTMLElementTagNameMap, Name extends keyof Map>(
nodes: ChildNodeArray,
name: Name,
attributes: { [key: string]: boolean },
create_element: (name: Name) => Map[Name]
): Map[Name]
function claim_element_base<Map extends SVGElementTagNameMap, Name extends keyof Map>(
nodes: ChildNodeArray,
name: Name,
attributes: { [key: string]: boolean },
create_element: (name: Name) => Map[Name]
): Map[Name]
function claim_element_base<Map extends HTMLElementTagNameMap, Name extends keyof HTMLElementTagNameMap>(
nodes: ChildNodeArray,
name: Name,
attributes: { [key: string]: boolean },
create_element: (name: Name) => Map[Name]
) {
return claim_node<Map[Name]>(
nodes,
(node): node is Element | SVGElement => node.nodeName === name,
(node): node is Map[Name] => node.nodeName === name,
(node) => {
const remove = [];
for (let j = 0; j < node.attributes.length; j++) {
@ -495,11 +497,11 @@ function claim_element_base(nodes: ChildNodeArray, name: string, attributes: { [
);
}
export function claim_element(nodes: ChildNodeArray, name: string, attributes: { [key: string]: boolean }) {
export function claim_element<N extends keyof HTMLElementTagNameMap>(nodes: ChildNodeArray, name: N, attributes: { [key: string]: boolean }) {
return claim_element_base(nodes, name, attributes, element);
}
export function claim_svg_element(nodes: ChildNodeArray, name: string, attributes: { [key: string]: boolean }) {
export function claim_svg_element<N extends keyof SVGElementTagNameMap>(nodes: ChildNodeArray, name: N, attributes: { [key: string]: boolean }) {
return claim_element_base(nodes, name, attributes, svg_element);
}
@ -745,7 +747,6 @@ export class HtmlTag {
this.c(html);
}
//@ts-ignore
this.i(anchor);
}

@ -1,5 +1,6 @@
import { Readable, Subscriber, Writable } from 'svelte/store';
import { SvelteComponent } from '../index.js';
import { T$$ } from './types.js';
export function noop() {}
@ -17,8 +18,8 @@ export function is_promise<T = any>(value: any): value is PromiseLike<T> {
return !!value && (typeof value === 'object' || typeof value === 'function') && typeof value.then === 'function';
}
export function add_location(element, file, line, column, char) {
element.__svelte_meta = {
export function add_location(element: Element, file: string | undefined, line: number, column: number, char: number) {
(element as Element & { __svelte_meta: any }).__svelte_meta = {
loc: { file, line, column, char }
};
}
@ -86,20 +87,22 @@ export function component_subscribe<T, S extends Readable<T>>(component: SvelteC
component.$$.on_destroy.push(subscribe(store, callback));
}
export function create_slot(definition, ctx, $$scope, fn: Function) {
// TODO: find out what types are expected here instead of using `any` everywhere
export function create_slot(definition: any[], ctx: any[], $$scope: T$$, fn: Function) {
if (definition) {
const slot_ctx = get_slot_context(definition, ctx, $$scope, fn);
return definition[0](slot_ctx);
}
}
function get_slot_context(definition, ctx, $$scope, fn: Function) {
function get_slot_context(definition: any[], ctx: any[], $$scope: T$$, fn: Function) {
return definition[1] && fn
? assign($$scope.ctx.slice(), definition[1](fn(ctx)))
: $$scope.ctx;
}
export function get_slot_changes(definition, $$scope, dirty, fn: Function) {
export function get_slot_changes(definition: any[], $$scope: T$$, dirty: number[], fn: Function) {
if (definition[2] && fn) {
const lets = definition[2](fn(dirty));
@ -117,25 +120,26 @@ export function get_slot_changes(definition, $$scope, dirty, fn: Function) {
return merged;
}
// @ts-expect-error TODO
return $$scope.dirty | lets;
}
return $$scope.dirty;
}
export function update_slot_base(slot, slot_definition, ctx, $$scope, slot_changes, get_slot_context_fn) {
export function update_slot_base(slot: any, slot_definition: any[], ctx: any[], $$scope: T$$, slot_changes: any, get_slot_context_fn: Function) {
if (slot_changes) {
const slot_context = get_slot_context(slot_definition, ctx, $$scope, get_slot_context_fn);
slot.p(slot_context, slot_changes);
}
}
export function update_slot(slot, slot_definition, ctx, $$scope, dirty, get_slot_changes_fn, get_slot_context_fn) {
export function update_slot(slot: any, slot_definition: any[], ctx: any[], $$scope: T$$, dirty: number[], get_slot_changes_fn: Function, get_slot_context_fn: Function) {
const slot_changes = get_slot_changes(slot_definition, $$scope, dirty, get_slot_changes_fn);
update_slot_base(slot, slot_definition, ctx, $$scope, slot_changes, get_slot_context_fn);
}
export function get_all_dirty_from_scope($$scope) {
export function get_all_dirty_from_scope($$scope: T$$) {
if ($$scope.ctx.length > 32) {
const dirty = [];
const length = $$scope.ctx.length / 32;
@ -190,6 +194,6 @@ export function has_prop<X extends {}, Y extends PropertyKey>(obj: X, prop: Y):
return Object.prototype.hasOwnProperty.call(obj, prop);
}
export function action_destroyer(action_result) {
export function action_destroyer(action_result: { destroy?: () => void } | undefined) {
return action_result && is_function(action_result.destroy) ? action_result.destroy : noop;
}

@ -25,7 +25,7 @@
"stripInternal": true,
// TODO: error all the things
// "strict": true,
//"strict": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,

Loading…
Cancel
Save