Skip to content

Commit

Permalink
Core: fix most of typescript issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanblinov2k17 committed Oct 4, 2024
1 parent dd3cf72 commit 215e9e3
Show file tree
Hide file tree
Showing 64 changed files with 355 additions and 149 deletions.
20 changes: 18 additions & 2 deletions packages/devextreme/js/__internal/core/m_action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,23 @@ import { isFunction, isPlainObject } from '@js/core/utils/type';
import { getWindow } from '@js/core/utils/window';

class Action {
constructor(action, config) {
_action: any;

_context: any;

_beforeExecute: any;

_afterExecute: any;

_component: any;

_validatingTargetName: any;

_excludeValidators: any;

static executors: any;

constructor(action, config?) {
config = config || {};
this._action = action;
this._context = config.context || getWindow();
Expand Down Expand Up @@ -124,7 +140,7 @@ const createValidatorByTargetElement = (condition) => (e) => {
e.cancel = true;
}
};

// @ts-expect-error expect name and executor
Action.registerExecutor({
disabled: {
validate: createValidatorByTargetElement(($target) => $target.is('@js/coredx-state-disabled, .dx-state-disabled *')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const registerComponent = function (name, namespace, ComponentClass) {
};

const registerRendererComponent = function (name, ComponentClass) {
// @ts-expect-error 'fn' does not exist on type '(selector?: string | Element | dxElementWrapper | undefined) => dxElementWrapper'
$.fn[name] = function (options) {
const isMemberInvoke = typeof options === 'string';
let result;
Expand Down
3 changes: 2 additions & 1 deletion packages/devextreme/js/__internal/core/m_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ const configMethod = (...args) => {

extend(config, newConfig);
};

// @ts-expect-error typescript cant see global
if (typeof DevExpress !== 'undefined' && DevExpress.config) {
// @ts-expect-error typescript cant see global
configMethod(DevExpress.config);
}

Expand Down
53 changes: 45 additions & 8 deletions packages/devextreme/js/__internal/core/m_dom_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,44 @@ const TEXT_NODE = 3;
const DOCUMENT_NODE = 9;
const DOCUMENT_FRAGMENT_NODE = 11;

const nativeDOMAdapterStrategy = {
export interface DomAdapter {
querySelectorAll: (element, selector) => any;
elementMatches: (element, selector) => any;
getActiveElement: (element?: HTMLElement | null) => HTMLElement;
getDocument: () => Document;
getDocumentElement: () => HTMLDocument & {
scrollLeft: number;
scrollTop: number;
clientWidth: number;
scrollHeight: number;
offsetHeight: number;
clientHeight: number;
};
getHead: () => any;
listen: (element, event, callback, options?) => any;
getReadyState: () => DocumentReadyState;
isNode: (node: unknown) => boolean;
isDocument: (element: any) => boolean;
isDocumentFragment: (element: any) => boolean;
getBody: () => HTMLBodyElement;
getRootNode: (element: HTMLElement) => Document | DocumentFragment;
getAttribute: (element, name) => any;
setAttribute: (element, name, value) => void;
removeAttribute: (element, name) => void;
isElementNode: (element: any) => boolean;
createElement: (tagName: string, context?: Document) => HTMLElement;
createDocumentFragment: () => DocumentFragment;
createTextNode: (text: any, context?: any) => any;
setClass: (element: HTMLElement, className: string, isAdd: boolean) => void;
setText: (element, text) => void;
setProperty: (element, name, value) => void;
removeElement: (element: HTMLElement) => void;
inject: (obj: Record<string, unknown>) => void;
setStyle: (element: HTMLElement, name: string, value: string) => void;
insertElement: (parentElement: HTMLElement, newElement: HTMLElement, nextSiblingElement?: HTMLElement) => void;
}

const nativeDOMAdapterStrategy: DomAdapter = {
querySelectorAll(element, selector) {
return element.querySelectorAll(selector);
},
Expand Down Expand Up @@ -36,8 +73,8 @@ const nativeDOMAdapterStrategy = {
},

createElement(tagName, context) {
context = context || this._document;
return context.createElement(tagName);
context = context ?? this._document;
return context!.createElement(tagName);
},

createElementNS(ns, tagName, context) {
Expand All @@ -56,11 +93,11 @@ const nativeDOMAdapterStrategy = {
},

isNode(element) {
return element && typeof element === 'object' && 'nodeType' in element && 'nodeName' in element;
return !!element && typeof element === 'object' && 'nodeType' in element && 'nodeName' in element;
},

isElementNode(element) {
return element && element.nodeType === ELEMENT_NODE;
return !!element && element.nodeType === ELEMENT_NODE;
},

isTextNode(element) {
Expand Down Expand Up @@ -141,7 +178,7 @@ const nativeDOMAdapterStrategy = {

return activeElementHolder.activeElement;
},

// @ts-expect-error return type is not assignable
getRootNode(element) {
return element?.getRootNode?.() ?? this._document;
},
Expand All @@ -166,7 +203,7 @@ const nativeDOMAdapterStrategy = {
return this._document.selection;
},

getReadyState() {
getReadyState(): DocumentReadyState {
return this._document.readyState;
},

Expand Down Expand Up @@ -201,5 +238,5 @@ const nativeDOMAdapterStrategy = {
},
};

const domAdapter = injector(nativeDOMAdapterStrategy);
const domAdapter: DomAdapter = injector(nativeDOMAdapterStrategy);
export { domAdapter };
10 changes: 5 additions & 5 deletions packages/devextreme/js/__internal/core/m_element_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const dataMap = new WeakMap();
let strategy;

export const strategyChanging = new MemorizedCallbacks();
let beforeCleanDataFunc = function () {};
let afterCleanDataFunc = function () {};
let beforeCleanDataFunc: any = function () {};
let afterCleanDataFunc: any = function () {};

export const setDataStrategy = function (value) {
strategyChanging.fire(value);
Expand Down Expand Up @@ -79,8 +79,8 @@ export function getDataStrategy() {
return strategy;
}

export function data() {
return strategy.data.apply(this, arguments);
export function data(...args) {
return strategy.data.apply(this, args);
}

export function beforeCleanData(callback) {
Expand All @@ -99,7 +99,7 @@ export function removeData(element, key) {
return strategy.removeData.call(this, element, key);
}

export function cleanDataRecursive(element, cleanSelf) {
export function cleanDataRecursive(element, cleanSelf?: boolean) {
if (!domAdapter.isElementNode(element)) {
return;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/devextreme/js/__internal/core/m_events_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { each } from '@js/core/utils/iterator';
import { isFunction, isPlainObject } from '@js/core/utils/type';

export class EventsStrategy {
_events: any;

_owner: any;

_options: any;

constructor(owner, options = {}) {
this._events = {};
this._owner = owner;
Expand Down
1 change: 1 addition & 0 deletions packages/devextreme/js/__internal/core/m_http_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const window = getWindow();

const nativeXMLHttpRequest = {
getXhr() {
// @ts-expect-error no XMLHttpRequest on Window
return new window.XMLHttpRequest();
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const infernoRenderer = injector({
createElement(component, props),
mountNode,
);
// @ts-expect-error need to change type to inferno element
container.$V = mountNode.$V;
if (parentNode) {
parentNode.insertBefore(container, nextNode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import Callbacks from '@js/core/utils/callbacks';
import { each } from '@js/core/utils/iterator';

import type { CallbackInterface } from './utils/m_callbacks';

class MemorizedCallbacks {
memory: any[];

callbacks: CallbackInterface;

constructor() {
this.memory = [];
this.callbacks = Callbacks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Deferred, when } from '@js/core/utils/deferred';
import { isDefined } from '@js/core/utils/type';

export class PostponedOperations {
_postponedOperations: any;

constructor() {
this._postponedOperations = {};
}
Expand All @@ -10,6 +12,7 @@ export class PostponedOperations {
if (key in this._postponedOperations) {
postponedPromise && this._postponedOperations[key].promises.push(postponedPromise);
} else {
// @ts-expect-error only void function can be called with new
const completePromise = new Deferred();
this._postponedOperations[key] = {
fn,
Expand Down
21 changes: 12 additions & 9 deletions packages/devextreme/js/__internal/core/m_renderer_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ const InitRender = function (selector, context) {
this.length = 1;
return this;
} if (Array.isArray(selector)) {
[].push.apply(this, selector);
([] as any[]).push.apply(this, selector);
return this;
}

return renderer(selector.toArray ? selector.toArray() : [selector]);
};

renderer = function (selector, context) {
// @ts-expect-error only void function can be called with new
return new InitRender(selector, context);
};
renderer.fn = { dxRenderer: true };
Expand Down Expand Up @@ -181,7 +182,7 @@ InitRender.prototype.html = function (value) {
return this.append(parseHTML(value));
};

const appendElements = function (element, nextSibling) {
const appendElements = function (element, nextSibling?) {
if (!this[0] || !element) return;

if (typeof element === 'string') {
Expand Down Expand Up @@ -378,7 +379,7 @@ InitRender.prototype.empty = function () {
};

InitRender.prototype.clone = function () {
const result = [];
const result: any[] = [];
for (let i = 0; i < this.length; i++) {
result.push(this[i].cloneNode(true));
}
Expand Down Expand Up @@ -425,7 +426,7 @@ InitRender.prototype.find = function (selector) {
return result;
}

const nodes = [];
const nodes: any[] = [];
let i;

if (typeof selector === 'string') {
Expand Down Expand Up @@ -477,7 +478,7 @@ InitRender.prototype.filter = function (selector) {
return this.filter((_, element) => !isVisible(_, element));
}

const result = [];
const result: any[] = [];
for (let i = 0; i < this.length; i++) {
const item = this[i];
if (domAdapter.isElementNode(item) && type(selector) === 'string') {
Expand All @@ -497,7 +498,7 @@ InitRender.prototype.filter = function (selector) {
};

InitRender.prototype.not = function (selector) {
const result = [];
const result: any[] = [];
const nodes = this.filter(selector).toArray();

for (let i = 0; i < this.length; i++) {
Expand All @@ -514,7 +515,7 @@ InitRender.prototype.is = function (selector) {
};

InitRender.prototype.children = function (selector) {
let result = [];
let result: any[] = [];
for (let i = 0; i < this.length; i++) {
const nodes = this[i] ? this[i].childNodes : [];
for (let j = 0; j < nodes.length; j++) {
Expand All @@ -535,7 +536,7 @@ InitRender.prototype.siblings = function () {
return renderer();
}

const result = [];
const result: any[] = [];
const parentChildNodes = element.parentNode.childNodes || [];

for (let i = 0; i < parentChildNodes.length; i++) {
Expand Down Expand Up @@ -597,7 +598,7 @@ InitRender.prototype.parent = function (selector) {
};

InitRender.prototype.parents = function (selector) {
const result = [];
const result: any[] = [];
let parent = this.parent();

while (parent && parent[0] && !domAdapter.isDocument(parent[0])) {
Expand Down Expand Up @@ -661,9 +662,11 @@ InitRender.prototype.add = function (selector) {

const emptyArray = [];
InitRender.prototype.splice = function () {
// @ts-expect-error get rid of arguments
return renderer(emptyArray.splice.apply(this, arguments));
};
InitRender.prototype.slice = function () {
// @ts-expect-error get rid of arguments
return renderer(emptyArray.slice.apply(this, arguments));
};
InitRender.prototype.toArray = function () {
Expand Down
8 changes: 8 additions & 0 deletions packages/devextreme/js/__internal/core/m_resize_observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ const ResizeObserverMock = {
};

class ResizeObserverSingleton {
_callbacksMap?: any;

_observer?: any;

constructor() {
// @ts-expect-error ResizeObserver doesn't exist on Window type
// we need to make our own for extensions like this
if (!hasWindow() || !window.ResizeObserver) {
// eslint-disable-next-line no-constructor-return
return ResizeObserverMock;
}

this._callbacksMap = new Map();
// @ts-expect-error ResizeObserver doesn't exist on Window type
this._observer = new window.ResizeObserver((entries) => {
entries.forEach((entry) => {
this._callbacksMap.get(entry.target)?.(entry);
Expand All @@ -43,6 +50,7 @@ class ResizeObserverSingleton {
const resizeObserverSingleton = new ResizeObserverSingleton();

/// #DEBUG
// @ts-expect-error singleton typing issue
resizeObserverSingleton.ResizeObserverSingleton = ResizeObserverSingleton;
/// #ENDDEBUG

Expand Down
Loading

0 comments on commit 215e9e3

Please sign in to comment.