Skip to content

Commit

Permalink
type fixes. close #1692
Browse files Browse the repository at this point in the history
  • Loading branch information
lavrton committed Dec 25, 2023
1 parent 813cd4f commit a8efcd5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export abstract class Container<
obj.children = [];

this.getChildren().forEach((child) => {
obj.children.push(child.toObject());
obj.children!.push(child.toObject());
});

return obj;
Expand Down
33 changes: 22 additions & 11 deletions src/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export interface NodeConfig {
opacity?: number;
scale?: Vector2d;
scaleX?: number;
skewX?: number;
skewY?: number;
scaleY?: number;
rotation?: number;
rotationDeg?: number;
Expand Down Expand Up @@ -911,7 +913,7 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
* @returns {Object}
*/
getAttrs() {
return this.attrs || {};
return (this.attrs || {}) as Config & Record<string, any>;
}
/**
* set multiple attrs at once using an object literal
Expand Down Expand Up @@ -1481,15 +1483,21 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
* @returns {Object}
*/
toObject() {
var obj = {} as any,
attrs = this.getAttrs(),
var attrs = this.getAttrs() as any,
key,
val,
getter,
defaultValue,
nonPlainObject;

obj.attrs = {};
const obj: {
attrs: Config & Record<string, any>;
className: string;
children?: Array<any>;
} = {
attrs: {} as Config & Record<string, any>,
className: this.getClassName(),
};

for (key in attrs) {
val = attrs[key];
Expand All @@ -1507,12 +1515,11 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
// restore attr value
attrs[key] = val;
if (defaultValue !== val) {
obj.attrs[key] = val;
(obj.attrs as any)[key] = val;
}
}

obj.className = this.getClassName();
return Util._prepareToStringify(obj);
return Util._prepareToStringify(obj) as typeof obj;
}
/**
* convert Node into a JSON string. Returns a JSON string.
Expand Down Expand Up @@ -2088,10 +2095,14 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
try {
const callback = config?.callback;
if (callback) delete config.callback;
this.toCanvas(config).toBlob((blob) => {
resolve(blob);
callback?.(blob);
}, config?.mimeType, config?.quality);
this.toCanvas(config).toBlob(
(blob) => {
resolve(blob);
callback?.(blob);
},
config?.mimeType,
config?.quality
);
} catch (err) {
reject(err);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ export const Util = {
});
return newStart;
},
_prepareToStringify(obj) {
_prepareToStringify<T>(obj: any): T | null {
var desc;

obj.visitedByCircularReferenceRemoval = true;
Expand Down

0 comments on commit a8efcd5

Please sign in to comment.