Skip to content

EspoCRM – Open Source CRM Application

Type fixes

Yurii097fe23212e6
diff --git a/client/src/collection.ts b/client/src/collection.ts
index e71859595f..1b9405ed5b 100644
--- a/client/src/collection.ts
+++ b/client/src/collection.ts
@@ -97,7 +97,7 @@ export interface Options {
model?: typeof Model;
defs: import('model').Defs;
maxSize?: number;
- entityType?: string;
+ entityType?: string | null;
urlRoot?: string;
url?: string;
orderBy: string | null;
@@ -174,7 +174,7 @@ export default class Collection<TModel extends Model = Model> {
/**
* A where function.
*/
- whereFunction: () => WhereItem[] | null = null
+ whereFunction: (() => WhereItem[]) | null = null
/**
* A last sync request promise.
@@ -219,7 +219,7 @@ export default class Collection<TModel extends Model = Model> {
private _byId: Record<string, TModel>
- private readonly _onModelEventBind: () => void;
+ private readonly _onModelEventBind: any;
/**
* @param {Model[]|Record<string, *>[]|null} [models] Models.
@@ -374,11 +374,7 @@ export default class Collection<TModel extends Model = Model> {
models = Array.isArray(models) ? models.slice() : [models];
- let at = options.at;
-
- if (at != null) {
- at = +at;
- }
+ let at = options.at ?? 0;
if (at > this.length) {
at = this.length;
@@ -388,11 +384,11 @@ export default class Collection<TModel extends Model = Model> {
at += this.length + 1;
}
- const set = [];
+ const set = [] as unknown[];
const toAdd = [];
const toMerge = [];
const toRemove = [];
- const modelMap = {};
+ const modelMap = {} as Record<string, any>;
const add = options.add;
const merge = options.merge;
@@ -401,6 +397,7 @@ export default class Collection<TModel extends Model = Model> {
let model: TModel | Record<string, any>;
for (let i = 0; i < models.length; i++) {
+ // @ts-ignore
model = models[i];
const existing = this._get(model);
@@ -423,8 +420,10 @@ export default class Collection<TModel extends Model = Model> {
set.push(existing);
}
+ // @ts-ignore
models[i] = existing;
} else if (add) {
+ // @ts-ignore
model = models[i] = this._prepareModel(model);
if (model && model instanceof Model) {
@@ -460,6 +459,7 @@ export default class Collection<TModel extends Model = Model> {
orderChanged =
this.length !== set.length ||
_.some(this.models, (m, index) => {
+ // @ts-ignore
return m !== set[index];
});
@@ -923,7 +923,7 @@ export default class Collection<TModel extends Model = Model> {
* @return {boolean}
*/
isBeingFetched(): boolean {
- return this.lastSyncPromise && this.lastSyncPromise.getReadyState() < 4;
+ return Boolean(this.lastSyncPromise && this.lastSyncPromise?.getReadyState() < 4);
}
/**
@@ -931,7 +931,7 @@ export default class Collection<TModel extends Model = Model> {
*/
abortLastFetch() {
if (this.isBeingFetched()) {
- this.lastSyncPromise.abort();
+ this.lastSyncPromise?.abort();
}
}
@@ -1042,8 +1042,10 @@ export default class Collection<TModel extends Model = Model> {
// noinspection JSUnusedGlobalSymbols
/**
* Compose a URL for syncing. Called from Model.sync.
+ *
+ * @internal
*/
- protected composeSyncUrl(): string {
+ protected composeSyncUrl(): string | null {
return this.url;
}
@@ -1163,6 +1165,7 @@ export default class Collection<TModel extends Model = Model> {
}
if (!model) {
+ // @ts-ignore
this.trigger.apply(this, arguments);
return;
@@ -1191,6 +1194,7 @@ export default class Collection<TModel extends Model = Model> {
}
}
+ // @ts-ignore
this.trigger.apply(this, arguments);
}
@@ -1210,8 +1214,8 @@ export default class Collection<TModel extends Model = Model> {
return new ModelClass(attributes, {
collection: this,
// @ts-ignore
- entityType: this.entityType || this.name,
- defs: this.defs,
+ entityType: this.entityType ?? this.name ?? undefined,
+ defs: this.defs ?? undefined,
});
}
diff --git a/client/src/global.d.ts b/client/src/global.d.ts
new file mode 100644
index 0000000000..de6d2bc183
--- /dev/null
+++ b/client/src/global.d.ts
@@ -0,0 +1,21 @@
+export {};
+
+/**
+ * A callback with resolved dependencies passed as parameters.
+ * Should return a value to define a module.
+ *
+ * @param {...any} arguments Resolved dependencies.
+ */
+type RequireCallback = (any) => void;
+
+type RequireErrorCallback = () => void;
+
+declare global {
+ namespace Espo {
+ // noinspection ES6ConvertVarToLetConst
+ var loader: {
+ require: (id: string, callback: RequireCallback, errorCallback?: RequireErrorCallback) => void;
+ requirePromise: (id: string) => Promise<unknown>;
+ }
+ }
+}
diff --git a/client/src/model-factory.ts b/client/src/model-factory.ts
index 1b8f63ef5a..6863147854 100644
--- a/client/src/model-factory.ts
+++ b/client/src/model-factory.ts
@@ -30,7 +30,6 @@
import Metadata from 'metadata';
import Model from 'model';
-import loader
/**
* A model factory.