simplify types. close #1988

This commit is contained in:
Anton Lavrevov
2025-10-22 14:48:36 -05:00
parent cbb951a1a1
commit 7e84e0e612
4 changed files with 24 additions and 14 deletions

View File

@@ -11,6 +11,7 @@ export type ClipFuncOutput =
| void
| [Path2D | CanvasFillRule]
| [Path2D, CanvasFillRule];
export interface ContainerConfig extends NodeConfig {
clearBeforeDraw?: boolean;
clipFunc?: (ctx: SceneContext) => ClipFuncOutput;
@@ -32,7 +33,7 @@ export interface ContainerConfig extends NodeConfig {
*/
export abstract class Container<
ChildType extends Node = Node,
Config extends ContainerConfig = ContainerConfig
Config extends ContainerConfig = ContainerConfig,
> extends Node<Config> {
children: Array<ChildType> = [];

View File

@@ -135,7 +135,7 @@ type globalCompositeOperationType =
| 'luminosity';
// allow any custom attribute
export type NodeConfig<Props extends Record<string, any> = {}> = Props & {
export type NodeConfig = {
x?: number;
y?: number;
width?: number;
@@ -161,7 +161,8 @@ export type NodeConfig<Props extends Record<string, any> = {}> = Props & {
preventDefault?: boolean;
globalCompositeOperation?: globalCompositeOperationType;
filters?: Filters;
}
[string: string]: any;
};
// CONSTANTS
const ABSOLUTE_OPACITY = 'absoluteOpacity',
@@ -1024,7 +1025,9 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
* @example
* var x = node.getAttr('x');
*/
getAttr<AttrConfig extends Config, K extends AnyString<keyof Config>>(attr: K): K extends keyof AttrConfig ? AttrConfig[K] : any {
getAttr<AttrConfig extends Config, K extends AnyString<keyof Config>>(
attr: K
): K extends keyof AttrConfig ? AttrConfig[K] : any {
const method = 'get' + Util._capitalize(attr as string);
if (Util._isFunction((this as any)[method])) {
return (this as any)[method]();
@@ -1060,7 +1063,7 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
* @returns {Object}
*/
getAttrs(): Config {
return (this.attrs || {});
return this.attrs || {};
}
/**
* set multiple attrs at once using an object literal
@@ -1663,7 +1666,7 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
delete attrs[key];
defaultValue = getter ? getter.call(this) : null;
// restore attr value
attrs[key] = val;
(attrs as any)[key] = val;
if (defaultValue !== val) {
(obj.attrs as any)[key] = val;
}
@@ -2399,7 +2402,10 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
* @example
* node.setAttr('x', 5);
*/
setAttr<AttrConfig extends Config, K extends AnyString<keyof Config>>(attr: K, val: K extends keyof AttrConfig ? AttrConfig[K] : any) {
setAttr<AttrConfig extends Config, K extends AnyString<keyof Config>>(
attr: K,
val: K extends keyof AttrConfig ? AttrConfig[K] : any
) {
const func = this[SET + Util._capitalize(attr as string)];
if (Util._isFunction(func)) {
@@ -2416,7 +2422,10 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
drawNode?.batchDraw();
}
}
_setAttr<AttrConfig extends Config, K extends AnyString<keyof Config>>(key: K, val: K extends keyof AttrConfig ? AttrConfig[K] : any) {
_setAttr<AttrConfig extends Config, K extends AnyString<keyof Config>>(
key: K,
val: K extends keyof AttrConfig ? AttrConfig[K] : any
) {
const oldVal = this.attrs[key];
if (oldVal === val && !Util.isObject(val)) {
return;
@@ -2872,7 +2881,7 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
}
}
interface AnimTo extends NodeConfig<Record<string, any>> {
interface AnimTo extends NodeConfig {
onFinish?: Function;
onUpdate?: Function;
duration?: number;

View File

@@ -26,7 +26,7 @@ export type ShapeConfigHandler<TTarget> = {
export type LineJoin = 'round' | 'bevel' | 'miter';
export type LineCap = 'butt' | 'round' | 'square';
export type ShapeConfig<Props extends Record<string, any> = {}> = NodeConfig<Props> & {
export type ShapeConfig = NodeConfig & {
fill?: string | CanvasGradient;
fillPatternImage?: HTMLImageElement;
fillPatternX?: number;
@@ -82,7 +82,7 @@ export type ShapeConfig<Props extends Record<string, any> = {}> = NodeConfig<Pro
dashOffset?: number;
dashEnabled?: boolean;
perfectDrawEnabled?: boolean;
}
};
export interface ShapeGetClientRectConfig {
skipTransform?: boolean;

View File

@@ -8,9 +8,9 @@ import type { GetSet } from '../types.ts';
import type { Context } from '../Context.ts';
import { getNumberOrArrayOfNumbersValidator } from '../Validators.ts';
export type RectConfig<Props extends Record<string, any> = {}> = ShapeConfig<Props> & {
export type RectConfig = ShapeConfig & {
cornerRadius?: number | number[];
}
};
/**
* Rect constructor
@@ -30,7 +30,7 @@ export type RectConfig<Props extends Record<string, any> = {}> = ShapeConfig<Pro
* strokeWidth: 5
* });
*/
export class Rect<Props extends Record<string, any> = {}> extends Shape<RectConfig<Props>> {
export class Rect extends Shape<RectConfig> {
_sceneFunc(context: Context) {
const cornerRadius = this.cornerRadius(),
width = this.width(),