Closed rnike closed 3 days ago
We are on iink-ts@2.0.0-alpha.6, and the type of element is not so friendly. Typescript cannot recognize the element type in switch case
iink-ts@2.0.0-alpha.6
Here is a rough example to make this library easier to use (I didn't look forward to the entire types, might miss something)
interface TJIIXBase { 'bounding-box'?: TBoundingBox; items?: TJIIXStrokeItem[]; } interface TJIIXElementBase<T = string> extends TJIIXBase { id: string; type: T; } interface TJIIXNodeElementBase<T extends ShapeKind> extends TJIIXElementBase<'Node'> { kind: T; } interface TJIIXNodeCircle extends TJIIXNodeElementBase<ShapeKind.Circle> { cx: number; cy: number; r: number; } interface TJIIXNodeEllipse extends TJIIXNodeElementBase<ShapeKind.Ellipse> { cx: number; cy: number; rx: number; ry: number; } interface TJIIXNodeRectangle extends TJIIXNodeElementBase<ShapeKind.Rectangle> { height: number; width: number; x: number; y: number; } interface TJIIXNodeTriangle extends TJIIXNodeElementBase<ShapeKind.Triangle> { id: string; type: 'Node'; kind: ShapeKind.Triangle; points: number[]; } interface TJIIXNodeParrallelogram extends TJIIXNodeElementBase<ShapeKind.Parallelogram> { points: number[]; } interface TJIIXNodePolygon extends TJIIXNodeElementBase<ShapeKind.Polygon> { points: number[]; } interface TJIIXNodeRhombus extends TJIIXNodeElementBase<ShapeKind.Rhombus> { points: number[]; } type TJIIXNodeElement = | TJIIXNodeCircle | TJIIXNodeEllipse | TJIIXNodeRectangle | TJIIXNodeTriangle | TJIIXNodeParrallelogram | TJIIXNodePolygon | TJIIXNodeRhombus; interface TJIIXTextElement extends TJIIXElementBase<'Text'> { 'bounding-box'?: TBoundingBox; label: string; words?: TJIIXWord[]; chars?: TJIIXChar[]; } interface TJIIXEdgeElementBase<T = EdgeKind> extends TJIIXElementBase<'Edge'> { kind: T; } interface TJIIXEdgeLine extends TJIIXEdgeElementBase<EdgeKind.Line> { kind: EdgeKind.Line; x1: number; x2: number; y1: number; y2: number; p1Decoration?: EdgeDecoration; p2Decoration?: EdgeDecoration; } interface TJIIXEdgeArc extends TJIIXEdgeElementBase<EdgeKind.Arc> { cx: number; cy: number; rx: number; ry: number; phi: number; startAngle: number; sweepAngle: number; startDecoration?: EdgeDecoration; endDecoration?: EdgeDecoration; } type TJIIXEdgeElement = TJIIXEdgeLine | TJIIXEdgeArc; type TJIIXElement = TJIIXNodeElement | TJIIXTextElement | TJIIXEdgeElement;
Using the types above, we can get the correct type in switch case or if statement using typescript
thank you very much for this suggestion, we will take it in the next version
New version (2.0.0) with your suggestion. Thank you for your feedback.
We are on
iink-ts@2.0.0-alpha.6
, and the type of element is not so friendly. Typescript cannot recognize the element type in switch caseHere is a rough example to make this library easier to use (I didn't look forward to the entire types, might miss something)
Using the types above, we can get the correct type in switch case or if statement using typescript