From dfcabd2834b117abab065bb770f3e855d6984793 Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Sat, 12 Oct 2024 14:10:08 -0400 Subject: [PATCH] Enable ts-strict for treeUtil.ts (#1238) --- src/utils/treeUtil.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/utils/treeUtil.ts b/src/utils/treeUtil.ts index 244bb9eb9..f2dd2f034 100644 --- a/src/utils/treeUtil.ts +++ b/src/utils/treeUtil.ts @@ -1,10 +1,6 @@ -// @ts-strict-ignore import type { TreeNode } from 'primevue/treenode' -export function buildTree( - items: T[], - key: string | ((item: T) => string[]) -): TreeNode { +export function buildTree(items: T[], key: (item: T) => string[]): TreeNode { const root: TreeNode = { key: 'root', label: 'root', @@ -16,7 +12,7 @@ export function buildTree( } for (const item of items) { - const keys = typeof key === 'string' ? item[key] : key(item) + const keys = key(item) let parent = root for (let i = 0; i < keys.length; i++) { const k = keys[i] @@ -33,7 +29,7 @@ export function buildTree( children: [] } map[id] = node - parent.children.push(node) + parent.children?.push(node) } parent = map[id] } @@ -63,7 +59,7 @@ export function sortedTree(node: TreeNode): TreeNode { if (node.children) { // Sort the children of the current node const sortedChildren = [...node.children].sort((a, b) => - a.label.localeCompare(b.label) + (a.label ?? '').localeCompare(b.label ?? '') ) // Recursively sort the children and add them to the new node newNode.children = []