From 7883f9b00106d97cd1e6abd6643e3a8798a13837 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Sun, 19 Feb 2023 07:25:18 -0500 Subject: [PATCH] [v9.3.x] Alerting: Use optional chaining for accessing frames (#63417) Co-authored-by: Gilles De Mey --- .../alerting/unified/components/expressions/util.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/public/app/features/alerting/unified/components/expressions/util.ts b/public/app/features/alerting/unified/components/expressions/util.ts index 61a6b097dd8..2a8407a6416 100644 --- a/public/app/features/alerting/unified/components/expressions/util.ts +++ b/public/app/features/alerting/unified/components/expressions/util.ts @@ -1,11 +1,20 @@ import { DataFrame, Labels, roundDecimals } from '@grafana/data'; +/** + * ⚠️ `frame.fields` could be an empty array ⚠️ + * + * TypeScript will NOT complain about it when accessing items via index signatures. + * Make sure to check for empty array or use optional chaining! + * + * see https://github.com/Microsoft/TypeScript/issues/13778 + */ + const getSeriesName = (frame: DataFrame): string => { - return frame.name ?? formatLabels(frame.fields[0].labels ?? {}); + return frame.name ?? formatLabels(frame.fields[0]?.labels ?? {}); }; const getSeriesValue = (frame: DataFrame) => { - const value = frame.fields[0].values.get(0); + const value = frame.fields[0]?.values.get(0); if (Number.isFinite(value)) { return roundDecimals(value, 5);