Skip to content

Commit ed333ae

Browse files
#10216 - Refactor: Explicit Any type clean up (part 5.1) (#10217)
* #10216 - Replace explicit any with typed/unknown alternatives (part 5.1) Remove explicit any from the KET serializers and editor/UI code: - serializers.types: default KetFileNode<T = unknown> + new KetFileRootContent; narrow the rgroup case in ketSerializer accordingly - multitailArrowToStruct / imageToStruct: type ketItem via the entity fromKetNode param types - multitailArrowsValidator: type json as KetFileRootContent - isMacroMolecule: type dragCtx via the existing DragContext - templatePreview: type the action promise as a tuple - closest: Map<number, number> for the merge reducer - Recognize / Check: ThunkDispatch for mapDispatchToProps; typed window.webkitURL The four remaining flagged locations (Tool.ts, Select.tsx, Tabs.types.ts, SettingsManager.ts) require refactors/dependency changes beyond a type swap and are split into a follow-up. * #10216 - Address svvald review comments - isMacroMolecule: simplify dragCtx param to DragContext (mergeItems is on the shared base; only caller passes a DragContext). - serializers.types: extract KetFileRoot interface instead of inlining root: { nodes }. - Recognize: extract a WindowWithWebkitURL type alias instead of the inline window cast. - ketcher-core actions/template: add force-keyed overloads to fromTemplateOnBondAction (sync tuple vs Promise) so templatePreview no longer casts its result.
1 parent 0a2c3be commit ed333ae

11 files changed

Lines changed: 86 additions & 46 deletions

File tree

packages/ketcher-core/src/application/editor/actions/template.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,26 @@ export function fromTemplateOnAtom(
222222
return [action, pasteItems];
223223
}
224224

225+
type FromTemplateOnBondResult = [Action, { atoms: number[]; bonds: number[] }];
226+
227+
export function fromTemplateOnBondAction(
228+
restruct,
229+
template,
230+
bid,
231+
events,
232+
flip,
233+
force: false,
234+
isPreview?: boolean,
235+
): FromTemplateOnBondResult;
236+
export function fromTemplateOnBondAction(
237+
restruct,
238+
template,
239+
bid,
240+
events,
241+
flip,
242+
force: true,
243+
isPreview?: boolean,
244+
): Promise<FromTemplateOnBondResult>;
225245
export function fromTemplateOnBondAction(
226246
restruct,
227247
template,

packages/ketcher-core/src/domain/serializers/ket/fromKet/imageToStruct.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
***************************************************************************/
1616

1717
import type { Struct } from 'domain/entities/struct';
18-
import { Image } from 'domain/entities/image';
18+
import { type KetFileImageNode, Image } from 'domain/entities/image';
1919

20-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
21-
export function imageToStruct(ketItem: any, struct: Struct): Struct {
20+
export function imageToStruct(
21+
ketItem: KetFileImageNode,
22+
struct: Struct,
23+
): Struct {
2224
struct.images.add(Image.fromKetNode(ketItem));
2325
return struct;
2426
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import type { Struct } from 'domain/entities/struct';
2-
import { MultitailArrow } from 'domain/entities/multitailArrow';
2+
import {
3+
type KetFileMultitailArrowNode,
4+
MultitailArrow,
5+
} from 'domain/entities/multitailArrow';
6+
import type { KetFileNode } from 'domain/serializers/serializers.types';
37

4-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5-
export function multitailArrowToStruct(ketItem: any, struct: Struct) {
8+
export function multitailArrowToStruct(
9+
ketItem: KetFileNode<KetFileMultitailArrowNode>,
10+
struct: Struct,
11+
) {
612
struct.addMultitailArrow(MultitailArrow.fromKetNode(ketItem));
713
return struct;
814
}

packages/ketcher-core/src/domain/serializers/ket/ketSerializer.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,9 @@ export class KetSerializer implements Serializer<Struct> {
206206
break;
207207
}
208208
case 'rgroup': {
209-
result.root.nodes.push({ $ref: `rg${item.data!.rgnumber}` });
210-
result[`rg${item.data!.rgnumber}`] = rgroupToKet(
211-
item.fragment!,
212-
item.data,
213-
);
209+
const { rgnumber } = item.data as { rgnumber: number };
210+
result.root.nodes.push({ $ref: `rg${rgnumber}` });
211+
result[`rg${rgnumber}`] = rgroupToKet(item.fragment!, item.data);
214212
break;
215213
}
216214
case 'plus': {

packages/ketcher-core/src/domain/serializers/ket/multitailArrowsValidator.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import type { KetFileNode } from 'domain/serializers/serializers.types';
1+
import type {
2+
KetFileNode,
3+
KetFileRootContent,
4+
} from 'domain/serializers/serializers.types';
25
import {
36
type KetFileMultitailArrowNode,
47
MultitailArrow,
58
} from 'domain/entities/multitailArrow';
69
import { MULTITAIL_ARROW_SERIALIZE_KEY } from 'domain/constants';
710

8-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9-
export const validateMultitailArrows = (json: any): boolean => {
11+
export const validateMultitailArrows = (json: KetFileRootContent): boolean => {
1012
const nodes: Array<KetFileNode<unknown>> = json.root.nodes;
1113
return nodes.every((node) => {
1214
if (node.type === MULTITAIL_ARROW_SERIALIZE_KEY) {

packages/ketcher-core/src/domain/serializers/serializers.types.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ export interface Serializer<T> {
2121
serialize: (struct: T) => string;
2222
}
2323

24-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
25-
export interface KetFileNode<T = any> {
24+
export interface KetFileNode<T = unknown> {
2625
type: string;
2726
fragment?: Struct;
2827
center: Vec2;
2928
data?: T;
3029
selected?: boolean;
3130
}
31+
32+
export interface KetFileRoot {
33+
nodes: KetFileNode[];
34+
}
35+
36+
export interface KetFileRootContent {
37+
root: KetFileRoot;
38+
}

packages/ketcher-react/src/script/editor/shared/closest.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -614,26 +614,24 @@ function findCloseMerge(
614614
mergeAtomToFunctionalGroup(atomId, restruct, atomPosition, result);
615615
});
616616
} else {
617-
result[map] = Array.from(pos[map].keys()).reduce(
618-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
619-
(res: Map<any, any>, srcId) => {
620-
const skip = { map, id: srcId };
621-
const item = findMaps[map](
622-
restruct,
623-
pos[map].get(srcId),
624-
skip,
625-
null,
626-
options,
627-
);
628-
629-
if (item && !selected[map].includes(item.id)) {
630-
res.set(srcId, item.id);
631-
}
632-
633-
return res;
634-
},
635-
new Map(),
636-
);
617+
result[map] = Array.from<number>(pos[map].keys()).reduce<
618+
Map<number, number>
619+
>((res, srcId) => {
620+
const skip = { map, id: srcId };
621+
const item = findMaps[map](
622+
restruct,
623+
pos[map].get(srcId),
624+
skip,
625+
null,
626+
options,
627+
);
628+
629+
if (item && !selected[map].includes(item.id)) {
630+
res.set(srcId, item.id);
631+
}
632+
633+
return res;
634+
}, new Map());
637635
}
638636
});
639637

packages/ketcher-react/src/script/editor/tool/helper/isMacroMolecule.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import type { Editor } from '../../Editor';
2+
import type { DragContext } from '../select/select.types';
23

34
const isMacroMolecule = (editor: Editor, id: number): boolean => {
45
const struct = editor.struct();
56
return struct.isFunctionalGroupFromMacromolecule(id);
67
};
78

8-
// dragCtx is actually "any" in the code
9-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
10-
const isMergingToMacroMolecule = (editor: Editor, dragCtx: any): boolean => {
9+
const isMergingToMacroMolecule = (
10+
editor: Editor,
11+
dragCtx: DragContext,
12+
): boolean => {
1113
const funcGroups = dragCtx?.mergeItems?.atomToFunctionalGroup;
1214
if (!funcGroups?.size) {
1315
return false;

packages/ketcher-react/src/script/editor/tool/templatePreview.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ class TemplatePreview {
196196
shouldFlip,
197197
true,
198198
true,
199-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
200-
) as Promise<any>;
199+
);
201200

202201
promise.then(([action, pasteItems]) => {
203202
if (!this.isModeFunctionalGroup) {

packages/ketcher-react/src/script/ui/views/modal/components/process/Check/Check.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import { type ComponentType, type FC, useEffect, useState } from 'react';
1818
import { connect } from 'react-redux';
19+
import type { AnyAction } from 'redux';
20+
import type { ThunkDispatch } from 'redux-thunk';
1921
import Form, {
2022
type FormState,
2123
Field,
@@ -312,7 +314,7 @@ const mapStateToProps = (state: State): CheckDialogStateProps => ({
312314
});
313315

314316
const mapDispatchToProps = (
315-
dispatch: any, // eslint-disable-line @typescript-eslint/no-explicit-any
317+
dispatch: ThunkDispatch<State, undefined, AnyAction>,
316318
ownProps: CheckDialogOwnProps,
317319
): CheckDialogDispatchProps => ({
318320
onCheck: (opts: CheckOption[]) =>

0 commit comments

Comments
 (0)