fix: resolve TypeScript narrowing errors and renamed lightweight-charts exports

This commit is contained in:
Marko Djordjevic 2026-02-16 16:05:54 +01:00
parent efcf5c185f
commit 23131b2c33
2 changed files with 34 additions and 25 deletions

View file

@ -763,13 +763,16 @@ const CandleChart = forwardRef<CandleChartHandle, CandleChartProps>(
const timestamp = typeof time === 'string' ? Date.parse(time) / 1000 : (time as number); const timestamp = typeof time === 'string' ? Date.parse(time) / 1000 : (time as number);
// First, check for line hit using primitives' hitTest // First, check for line hit using primitives' hitTest
let deleteLineHit: { id: number; primitive: TrendLine } | null = null; const deleteLineHit = (() => {
linePrimitivesRef.current.forEach((primitive, id) => { let result: { id: number; primitive: TrendLine } | null = null;
const hit = primitive.hitTest(timeCoordinate, priceCoordinate); linePrimitivesRef.current.forEach((primitive, id) => {
if (hit) { const hit = primitive.hitTest(timeCoordinate, priceCoordinate);
deleteLineHit = { id, primitive }; if (hit) {
} result = { id, primitive };
}); }
});
return result as { id: number; primitive: TrendLine } | null;
})();
if (deleteLineHit) { if (deleteLineHit) {
// Delete the clicked line // Delete the clicked line
@ -794,13 +797,16 @@ const CandleChart = forwardRef<CandleChartHandle, CandleChartProps>(
} }
// Next, check for rectangle hit using primitives' hitTest // Next, check for rectangle hit using primitives' hitTest
let rectangleHit: { id: number; primitive: RectangleDrawingPrimitive } | null = null; const rectangleHit = (() => {
rectanglePrimitivesRef.current.forEach((primitive, id) => { let result: { id: number; primitive: RectangleDrawingPrimitive } | null = null;
const hit = primitive.hitTest(timeCoordinate, priceCoordinate); rectanglePrimitivesRef.current.forEach((primitive, id) => {
if (hit) { const hit = primitive.hitTest(timeCoordinate, priceCoordinate);
rectangleHit = { id, primitive }; if (hit) {
} result = { id, primitive };
}); }
});
return result as { id: number; primitive: RectangleDrawingPrimitive } | null;
})();
if (rectangleHit) { if (rectangleHit) {
// Delete the clicked rectangle // Delete the clicked rectangle
@ -853,13 +859,16 @@ const CandleChart = forwardRef<CandleChartHandle, CandleChartProps>(
// Handle line selection when no tool is active or delete tool is active // Handle line selection when no tool is active or delete tool is active
if (!activeTool || activeTool === 'delete') { if (!activeTool || activeTool === 'delete') {
// Check if a line was clicked // Check if a line was clicked
let lineHit: { id: number; primitive: TrendLine } | null = null; const lineHit = (() => {
linePrimitivesRef.current.forEach((primitive, id) => { let result: { id: number; primitive: TrendLine } | null = null;
const hit = primitive.hitTest(timeCoordinate, priceCoordinate); linePrimitivesRef.current.forEach((primitive, id) => {
if (hit && activeTool !== 'delete') { const hit = primitive.hitTest(timeCoordinate, priceCoordinate);
lineHit = { id, primitive }; if (hit && activeTool !== 'delete') {
} result = { id, primitive };
}); }
});
return result as { id: number; primitive: TrendLine } | null;
})();
if (lineHit && activeTool !== 'delete') { if (lineHit && activeTool !== 'delete') {
// Toggle selection // Toggle selection

View file

@ -5,8 +5,8 @@ import {
IChartApi, IChartApi,
ISeriesApi, ISeriesApi,
ISeriesPrimitive, ISeriesPrimitive,
IPrimitivePaneRenderer, ISeriesPrimitivePaneRenderer,
IPrimitivePaneView, ISeriesPrimitivePaneView,
Logical, Logical,
SeriesOptionsMap, SeriesOptionsMap,
SeriesType, SeriesType,
@ -15,7 +15,7 @@ import {
PrimitiveHoveredItem, PrimitiveHoveredItem,
} from 'lightweight-charts'; } from 'lightweight-charts';
class TrendLinePaneRenderer implements IPrimitivePaneRenderer { class TrendLinePaneRenderer implements ISeriesPrimitivePaneRenderer {
_p1: ViewPoint; _p1: ViewPoint;
_p2: ViewPoint; _p2: ViewPoint;
_text1: string; _text1: string;
@ -117,7 +117,7 @@ interface ViewPoint {
y: Coordinate | null; y: Coordinate | null;
} }
class TrendLinePaneView implements IPrimitivePaneView { class TrendLinePaneView implements ISeriesPrimitivePaneView {
_source: TrendLine; _source: TrendLine;
_p1: ViewPoint = { x: null, y: null }; _p1: ViewPoint = { x: null, y: null };
_p2: ViewPoint = { x: null, y: null }; _p2: ViewPoint = { x: null, y: null };