Skip to content

Commit

Permalink
fix(build): upgrading to TypeScript 4.9 brought new build issue (#816)
Browse files Browse the repository at this point in the history
* fix(build): upgrading to TypeScript 4.9 brought new build issue

* chore: convert more object any to keyof
  • Loading branch information
ghiscoding committed Nov 17, 2022
1 parent 41cfe54 commit 4d46d8a
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 52 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -66,7 +66,7 @@
"rxjs": "^7.5.7",
"serve": "^14.1.1",
"ts-jest": "^29.0.3",
"typescript": "^4.8.4"
"typescript": "^4.9.3"
},
"packageManager": "pnpm@7.15.0",
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/formatters/formatterUtilities.ts
Expand Up @@ -139,7 +139,7 @@ export function parseFormatterWhenExist<T = any>(formatter: Formatter<T> | undef
fieldProperty = (props.length > 0) ? props[0] : columnDef.field;
}

const cellValue = (dataContext as any).hasOwnProperty(fieldProperty) ? (dataContext as any)[fieldProperty] : null;
const cellValue = dataContext?.hasOwnProperty(fieldProperty as keyof T) ? dataContext[fieldProperty as keyof T] : null;

if (typeof formatter === 'function') {
const formattedData = formatter(row, col, cellValue, columnDef, dataContext, grid);
Expand All @@ -151,7 +151,7 @@ export function parseFormatterWhenExist<T = any>(formatter: Formatter<T> | undef
output = '';
}
} else {
output = (!(dataContext as any).hasOwnProperty(fieldProperty)) ? '' : cellValue;
output = ((!dataContext?.hasOwnProperty(fieldProperty as keyof T)) ? '' : cellValue) as string;
if (output === null || output === undefined) {
output = '';
}
Expand Down
16 changes: 8 additions & 8 deletions packages/common/src/services/collection.service.ts
Expand Up @@ -59,29 +59,29 @@ export class CollectionService<T = any> {
switch (operator) {
case OperatorType.equal:
if (objectProperty) {
filteredCollection = collection.filter((item) => (item as any)[objectProperty] === value);
filteredCollection = collection.filter((item) => item[objectProperty as keyof T] === value);
} else {
filteredCollection = collection.filter((item) => item === value);
}
break;
case OperatorType.contains:
if (objectProperty) {
filteredCollection = collection.filter((item) => (item as any)[objectProperty].toString().indexOf(value.toString()) !== -1);
filteredCollection = collection.filter((item) => item[objectProperty as keyof T]?.toString().indexOf(value.toString()) !== -1);
} else {
filteredCollection = collection.filter((item: any) => (item !== null && item !== undefined) && item.toString().indexOf(value.toString()) !== -1);
}
break;
case OperatorType.notContains:
if (objectProperty) {
filteredCollection = collection.filter((item) => (item as any)[objectProperty].toString().indexOf(value.toString()) === -1);
filteredCollection = collection.filter((item) => item[objectProperty as keyof T]?.toString().indexOf(value.toString()) === -1);
} else {
filteredCollection = collection.filter((item: any) => (item !== null && item !== undefined) && item.toString().indexOf(value.toString()) === -1);
}
break;
case OperatorType.notEqual:
default:
if (objectProperty) {
filteredCollection = collection.filter((item) => (item as any)[objectProperty] !== value);
filteredCollection = collection.filter((item) => item[objectProperty as keyof T] !== value);
} else {
filteredCollection = collection.filter((item) => item !== value);
}
Expand Down Expand Up @@ -116,8 +116,8 @@ export class CollectionService<T = any> {
const sortDirection = sortBy.sortDesc ? SortDirectionNumber.desc : SortDirectionNumber.asc;
const objectProperty = sortBy.property;
const fieldType = sortBy?.fieldType ?? columnDef?.type ?? FieldType.string;
const value1 = (enableTranslateLabel) ? this.translaterService?.translate && this.translaterService.translate((dataRow1 as any)[objectProperty] || ' ') : (dataRow1 as any)[objectProperty];
const value2 = (enableTranslateLabel) ? this.translaterService?.translate && this.translaterService.translate((dataRow2 as any)[objectProperty] || ' ') : (dataRow2 as any)[objectProperty];
const value1 = (enableTranslateLabel) ? this.translaterService?.translate && this.translaterService.translate((dataRow1[objectProperty as keyof T] || ' ') as string) : dataRow1[objectProperty as keyof T];
const value2 = (enableTranslateLabel) ? this.translaterService?.translate && this.translaterService.translate((dataRow2[objectProperty as keyof T] || ' ') as string) : dataRow2[objectProperty as keyof T];

const sortResult = sortByFieldType(fieldType, value1, value2, sortDirection, columnDef);
if (sortResult !== SortDirectionNumber.neutral) {
Expand All @@ -135,8 +135,8 @@ export class CollectionService<T = any> {
const fieldType = sortByOptions?.fieldType ?? columnDef?.type ?? FieldType.string;

sortedCollection = collection.sort((dataRow1: T, dataRow2: T) => {
const value1 = (enableTranslateLabel) ? this.translaterService?.translate && this.translaterService.translate((dataRow1 as any)[objectProperty] || ' ') : (dataRow1 as any)[objectProperty];
const value2 = (enableTranslateLabel) ? this.translaterService?.translate && this.translaterService.translate((dataRow2 as any)[objectProperty] || ' ') : (dataRow2 as any)[objectProperty];
const value1 = (enableTranslateLabel) ? this.translaterService?.translate && this.translaterService.translate((dataRow1[objectProperty as keyof T] || ' ') as string) : dataRow1[objectProperty as keyof T];
const value2 = (enableTranslateLabel) ? this.translaterService?.translate && this.translaterService.translate((dataRow2[objectProperty as keyof T] || ' ') as string) : dataRow2[objectProperty as keyof T];
const sortResult = sortByFieldType(fieldType, value1, value2, sortDirection, columnDef);
if (sortResult !== SortDirectionNumber.neutral) {
return sortResult;
Expand Down
14 changes: 7 additions & 7 deletions packages/common/src/services/grid.service.ts
Expand Up @@ -402,7 +402,7 @@ export class GridService {
throw new Error('We could not find SlickGrid Grid, DataView objects');
}
const idPropName = this._gridOptions.datasetIdPropertyName || 'id';
if (!options?.skipError && (!item || !(idPropName in item))) {
if (!options?.skipError && (!item || !item.hasOwnProperty(idPropName))) {
throw new Error(`Adding an item requires the item to include an "${idPropName}" property`);
}

Expand Down Expand Up @@ -506,7 +506,7 @@ export class GridService {

// get row numbers of all new inserted items
// we need to do it after resort and get each row number because it possibly changed after the sort
items.forEach((item: T) => rowNumbers.push(this._dataView.getRowById((item as any)[idPropName]) as number));
items.forEach((item: T) => rowNumbers.push(this._dataView.getRowById(item[idPropName as keyof T] as string | number) as number));

// if user wanted to see highlighted row
if (options.highlightRow) {
Expand Down Expand Up @@ -536,10 +536,10 @@ export class GridService {
options = { ...GridServiceDeleteOptionDefaults, ...options };
const idPropName = this._gridOptions.datasetIdPropertyName || 'id';

if (!options?.skipError && (!item || !(idPropName in item))) {
if (!options?.skipError && (!item || !item.hasOwnProperty(idPropName))) {
throw new Error(`Deleting an item requires the item to include an "${idPropName}" property`);
}
return this.deleteItemById((item as any)[idPropName], options);
return this.deleteItemById(item[idPropName as keyof T] as string | number, options);
}

/**
Expand Down Expand Up @@ -650,7 +650,7 @@ export class GridService {
updateItem<T = any>(item: T, options?: GridServiceUpdateOption): number | undefined {
options = { ...GridServiceUpdateOptionDefaults, ...options };
const idPropName = this._gridOptions.datasetIdPropertyName || 'id';
const itemId = (!item || !(idPropName in item)) ? undefined : (item as any)[idPropName];
const itemId = (!item || !item.hasOwnProperty(idPropName)) ? undefined : (item as any)[idPropName];

if (!options?.skipError && itemId === undefined) {
throw new Error(`Calling Update of an item requires the item to include an "${idPropName}" property`);
Expand Down Expand Up @@ -682,7 +682,7 @@ export class GridService {
const rowNumbers: number[] = [];
const itemIds: Array<string | number> = [];
items.forEach((item: T) => {
const itemId = (!item || !(idPropName in item)) ? undefined : (item as any)[idPropName];
const itemId = (!item || !item.hasOwnProperty(idPropName)) ? undefined : (item as any)[idPropName];
itemIds.push(itemId);

if (this._dataView.getIdxById(itemId) !== undefined) {
Expand Down Expand Up @@ -786,7 +786,7 @@ export class GridService {
upsertItem<T = any>(item: T, options?: GridServiceInsertOption): { added: number | undefined; updated: number | undefined; } {
options = { ...GridServiceInsertOptionDefaults, ...options };
const idPropName = this._gridOptions.datasetIdPropertyName || 'id';
const itemId = (!item || !(idPropName in item)) ? undefined : (item as any)[idPropName];
const itemId = (!item || !item.hasOwnProperty(idPropName)) ? undefined : (item as any)[idPropName];

if (!options?.skipError && itemId === undefined) {
throw new Error(`Calling Upsert of an item requires the item to include an "${idPropName}" property`);
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/services/utilities.ts
Expand Up @@ -173,13 +173,13 @@ export function findItemInTreeStructure<T = any>(treeArray: T[], predicate: (ite
throw new Error('findRecursive requires parameter "childrenPropertyName"');
}
const initialFind = treeArray.find(predicate);
const elementsWithChildren = treeArray.filter((x: T) => childrenPropertyName in x && (x as any)[childrenPropertyName]);
const elementsWithChildren = treeArray.filter((x: T) => x?.hasOwnProperty(childrenPropertyName) && x[childrenPropertyName as keyof T]);
if (initialFind) {
return initialFind;
} else if (elementsWithChildren.length) {
const childElements: T[] = [];
elementsWithChildren.forEach((item: T) => {
if (childrenPropertyName in item) {
if (item?.hasOwnProperty(childrenPropertyName)) {
childElements.push(...(item as any)[childrenPropertyName]);
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/utils.ts
Expand Up @@ -7,7 +7,7 @@
export function addToArrayWhenNotExists<T = any>(inputArray: T[], inputItem: T, itemIdPropName = 'id') {
let arrayRowIndex = -1;
if (inputItem && typeof inputItem === 'object' && itemIdPropName in inputItem) {
arrayRowIndex = inputArray.findIndex((item) => (item as any)[itemIdPropName] === (inputItem as any)[itemIdPropName]);
arrayRowIndex = inputArray.findIndex((item) => item[itemIdPropName as keyof T] === inputItem[itemIdPropName as keyof T]);
} else {
arrayRowIndex = inputArray.findIndex((item) => item === inputItem);
}
Expand Down
Expand Up @@ -448,7 +448,7 @@ export class SlickVanillaGridBundle {

if (this.backendServiceApi) {
for (const prop of Object.keys(this.backendServiceApi)) {
(this.backendServiceApi as any)[prop] = null;
this.backendServiceApi[prop as keyof BackendServiceApi] = null;
}
this.backendServiceApi = undefined;
}
Expand Down
66 changes: 36 additions & 30 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4d46d8a

Please sign in to comment.