From fac15d45feb85650f708b2a2d66a7329d6059b0c Mon Sep 17 00:00:00 2001 From: Ivan Sedykh Date: Wed, 23 Jun 2021 09:12:09 +0500 Subject: [PATCH 1/3] types update --- src/dc-base-component.ts | 25 ++++++++++++++----------- src/dc-dom.ts | 16 ++++++++-------- src/index.ts | 3 ++- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/dc-base-component.ts b/src/dc-base-component.ts index 9f8d974..8d94f51 100644 --- a/src/dc-base-component.ts +++ b/src/dc-base-component.ts @@ -1,10 +1,10 @@ import { getElementOptions, - getElementRefs, ReferencesCollection, + getElementRefs, IDCRefsCollection, } from './dc-dom'; /** - * @typedef {Object.)}>} ReferencesCollection + * @typedef {Object.)}>} IDCRefsCollection */ /** @@ -47,16 +47,19 @@ interface DcListener { * @implements {DcBaseComponent} */ +type IDCOptions = { + [key in string | number]: any; +}; -class DcBaseComponent { - public readonly element: HTMLElement; +class DcBaseComponent { + public readonly element: Root; private _listenersList: DcListener[] = []; - protected readonly options: object; - protected readonly refs: ReferencesCollection; + protected readonly options: Options; + protected readonly refs: Refs; public static namespace: string = ''; public static requiredRefs: string[] = []; - public constructor(element: HTMLElement, namespace: string, requiredRefs: string[]) { + public constructor(element: Root, namespace: string, requiredRefs: string[]) { Object.freeze(this.addListener); Object.freeze(this.destroy); /** @@ -69,12 +72,12 @@ class DcBaseComponent { * Store an object with any data/settings which is provided by backend side * @type {Object} */ - this.options = getElementOptions(element, namespace); + this.options = getElementOptions(element, namespace); /** - * @type {ReferencesCollection} + * @type {IDCRefsCollection} */ - this.refs = getElementRefs( + this.refs = getElementRefs( element, namespace, ); @@ -84,7 +87,7 @@ class DcBaseComponent { private _checkRequiredRefs = (refs: string[], namespace: string): void => { refs.forEach(name => { - if (!this.refs[name]) { + if (!(this.refs as IDCRefsCollection)[name]) { throw new Error( `required ref "${name}" not found in ${namespace}` ); diff --git a/src/dc-dom.ts b/src/dc-dom.ts index c281b87..c6b7481 100644 --- a/src/dc-dom.ts +++ b/src/dc-dom.ts @@ -74,22 +74,22 @@ const isElementWithinLazyParent = (element: HTMLElement): boolean => { /** * @param {HTMLElement} element * @param {string} namespace - * @return {ReferencesCollection} + * @return {IDCRefsCollection} */ -interface ReferencesCollection { +interface IDCRefsCollection { [key: string]: HTMLElement | HTMLElement[] | { [_key: string]: HTMLElement }; } -const getElementRefs = (element: HTMLElement, namespace: string): ReferencesCollection => { +const getElementRefs = (element: HTMLElement, namespace: string): T => { const refAttribute = `${DC_NAMESPACE}-${namespace}-${DC_NAMESPACED_ATTRIBUTE_REFERENCE}`; const refSelector = `[${refAttribute}]`; const componentSelector = `[${getNamespacedAnchorAttribute(namespace)}]`; const nestedComponents = scopedQuerySelectorAll(element, componentSelector); - const refs: ReferencesCollection = {}; + const refs: IDCRefsCollection = {}; scopedQuerySelectorAll(element, refSelector) ?.filter((ref) => !nestedComponents.some((nested) => nested.contains(ref))) ?.forEach((ref) => { @@ -116,7 +116,7 @@ const getElementRefs = (element: HTMLElement, namespace: string): ReferencesColl } }); - return refs; + return refs as T; }; /** @@ -125,7 +125,7 @@ const getElementRefs = (element: HTMLElement, namespace: string): ReferencesColl * @return {?Object} */ -const getElementOptions = (element: HTMLElement, namespace: string) => { +const getElementOptions = (element: HTMLElement, namespace: string): T => { const attribute = getNamespacedAnchorAttribute(namespace); let result = {}; const attributeValue = element.getAttribute(attribute); @@ -138,7 +138,7 @@ const getElementOptions = (element: HTMLElement, namespace: string) => { } } - return result; + return result as T; }; const getNamespacedAnchorAttribute = (namespace: string): string => { @@ -146,11 +146,11 @@ const getNamespacedAnchorAttribute = (namespace: string): string => { }; export { - ReferencesCollection, scopedQuerySelectorAll, getElementRefs, findElementsForInit, isElementWithinLazyParent, matches, getElementOptions, + IDCRefsCollection, }; diff --git a/src/index.ts b/src/index.ts index 67e38fc..2666316 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import { DcBaseComponent } from './dc-base-component'; import { dcFactory } from './dc-factory'; import { dcAsync } from './dc-async'; +import { IDCRefsCollection } from './dc-dom'; -export { DcBaseComponent, dcFactory, dcAsync }; +export { DcBaseComponent, dcFactory, dcAsync, IDCRefsCollection }; From 1fd2afefc6db6880f5159c98713f1f4861084e54 Mon Sep 17 00:00:00 2001 From: Ivan Sedykh Date: Mon, 6 Dec 2021 11:43:56 +0500 Subject: [PATCH 2/3] quick fix for path --- src/dc-factory.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dc-factory.ts b/src/dc-factory.ts index 5318fca..82dab8d 100644 --- a/src/dc-factory.ts +++ b/src/dc-factory.ts @@ -1,5 +1,5 @@ import { findElementsForInit, isElementWithinLazyParent } from './dc-dom'; -import { DcBaseComponent } from 'src/dc-base-component' +import { DcBaseComponent } from './dc-base-component' import { states } from "./enums"; interface StatedComponent { From 678709f9aec0445776d75862de79195faf487df1 Mon Sep 17 00:00:00 2001 From: Ivan Sedykh Date: Mon, 6 Dec 2021 12:16:04 +0500 Subject: [PATCH 3/3] error any type --- src/dc-factory.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dc-factory.ts b/src/dc-factory.ts index 82dab8d..ffb1c85 100644 --- a/src/dc-factory.ts +++ b/src/dc-factory.ts @@ -117,7 +117,7 @@ class DcFactory { requestAnimationFrame(() => { try { this._createComponent(element, rComponent); - } catch (error) { + } catch (error: any) { this._setState(element, rComponent, states.ERROR); console.error(`Component ${_Class.name} hasn't been created due to error: ${error.message}`, element); console.error(error);