diff --git a/src/runtime/internal/dom.ts b/src/runtime/internal/dom.ts index e2ef368cf1..bd00f88da3 100644 --- a/src/runtime/internal/dom.ts +++ b/src/runtime/internal/dom.ts @@ -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(obj: T, exclude: return target; } -export function svg_element(name: K): SVGElement { +export function svg_element(name: K): SVGElementTagNameMap[K] { return document.createElementNS('http://www.w3.org/2000/svg', name); } @@ -296,27 +280,27 @@ export function listen(node: EventTarget, event: string, handler: EventListenerO } export function prevent_default(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(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(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(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(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( +function claim_element_base( + nodes: ChildNodeArray, + name: Name, + attributes: { [key: string]: boolean }, + create_element: (name: Name) => Map[Name] +): Map[Name] + +function claim_element_base( + nodes: ChildNodeArray, + name: Name, + attributes: { [key: string]: boolean }, + create_element: (name: Name) => Map[Name] +): Map[Name] + +function claim_element_base( + nodes: ChildNodeArray, + name: Name, + attributes: { [key: string]: boolean }, + create_element: (name: Name) => Map[Name] +) { + return claim_node( 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(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(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); } diff --git a/src/runtime/internal/utils.ts b/src/runtime/internal/utils.ts index 01cf1e4169..4351fe72cb 100644 --- a/src/runtime/internal/utils.ts +++ b/src/runtime/internal/utils.ts @@ -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(value: any): value is PromiseLike { 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>(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(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; } diff --git a/tsconfig.json b/tsconfig.json index 56be920ecb..39532bfe48 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,7 +25,7 @@ "stripInternal": true, // TODO: error all the things - // "strict": true, + //"strict": true, "noImplicitThis": true, "noUnusedLocals": true, "noUnusedParameters": true,