From 14e0e825b2c01247e610666087328e6fbd4c785c Mon Sep 17 00:00:00 2001 From: Victor Lin <13424970+victorlin@users.noreply.github.com> Date: Wed, 13 Nov 2024 15:56:08 -0800 Subject: [PATCH] _setDisplayName: Replace optional chaining with proper error handing With the current code, it is not possible for resources[i-1] to be undefined. Make this clear with type narrowing. --- static-site/src/components/ListResources/ResourceGroup.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/static-site/src/components/ListResources/ResourceGroup.tsx b/static-site/src/components/ListResources/ResourceGroup.tsx index 079e86536..5a9345aba 100644 --- a/static-site/src/components/ListResources/ResourceGroup.tsx +++ b/static-site/src/components/ListResources/ResourceGroup.tsx @@ -265,7 +265,11 @@ function _setDisplayName(resources: Resource[]): DisplayNamedResource[] { if (i===0) { name = r.nameParts.join(sep); } else { - let matchIdx = r.nameParts.map((word, j) => word === resources[i-1]?.nameParts[j]).findIndex((v) => !v); + const previousResource = resources[i-1]; + if (previousResource === undefined) { + throw new InternalError("Previous resource is undefined. Check that this is not run on i===0."); + } + let matchIdx = r.nameParts.map((word, j) => word === previousResource.nameParts[j]).findIndex((v) => !v); if (matchIdx===-1) { // -1 means every word is in the preceding name, but we should display the last word anyway matchIdx = r.nameParts.length-2; }