From 7f6bd3ee8116c17d98295a53f096eb874b93b4f8 Mon Sep 17 00:00:00 2001 From: Robin Wieruch Date: Thu, 23 Feb 2023 10:38:46 +0100 Subject: [PATCH] fix(TypeScript): tablieList => item is always ExtendedNode instead of TableNode --- .storybook/stories/Types/data.story.mdx | 7 +++++-- src/common/util/modifiers.ts | 4 ++-- src/common/util/tree/fromTreeToList.ts | 26 +++++++++++-------------- src/pagination/usePagination.ts | 4 ++-- src/sort/useSort.tsx | 4 ++-- src/tree/useTree.tsx | 3 ++- src/types/common.ts | 4 +++- src/types/table.ts | 7 +++++++ 8 files changed, 34 insertions(+), 25 deletions(-) diff --git a/.storybook/stories/Types/data.story.mdx b/.storybook/stories/Types/data.story.mdx index ef6596e..3b09c52 100644 --- a/.storybook/stories/Types/data.story.mdx +++ b/.storybook/stories/Types/data.story.mdx @@ -13,8 +13,11 @@ export type TableNode = { [prop: string]: any, }; -export type ExtendedTableNode = TableNode & { - ancestors: TableNode[], +export type ExtendedNode = T & { + treeXLevel?: number; + treeYLevel?: number; + parentNode?: ExtendedNode | Nullish; + ancestors?: ExtendedNode[]; }; export type Data = { diff --git a/src/common/util/modifiers.ts b/src/common/util/modifiers.ts index f841924..39e038d 100644 --- a/src/common/util/modifiers.ts +++ b/src/common/util/modifiers.ts @@ -1,9 +1,9 @@ -import { TableNode, Features } from '@table-library/react-table-library/types/table'; +import { TableNode, Features, ExtendedNode } from '@table-library/react-table-library/types/table'; export const applyModifiers = ({ sort, pagination, tree }: Features) => (nodes: TableNode[]) => { - let modifiedNodes = [...nodes]; + let modifiedNodes: ExtendedNode[] = [...nodes]; modifiedNodes = sort ? sort.modifier(modifiedNodes) : modifiedNodes; diff --git a/src/common/util/tree/fromTreeToList.ts b/src/common/util/tree/fromTreeToList.ts index 2b47b65..cdd3d98 100644 --- a/src/common/util/tree/fromTreeToList.ts +++ b/src/common/util/tree/fromTreeToList.ts @@ -1,15 +1,8 @@ import { Nullish } from '@table-library/react-table-library/types/common'; -import { Data, TableNode } from '@table-library/react-table-library/types/table'; +import { Data, TableNode, ExtendedNode } from '@table-library/react-table-library/types/table'; import { hasLeaves } from './hasLeaves'; -type ExtendedNode = { - treeXLevel: number; - treeYLevel: number; - parentNode: TableNode; - ancestors: TableNode[]; -}; - export const fromTreeToList = (nodes: T[] | Nullish): T[] => (nodes || []).reduce((acc: T[], value: T) => { acc = acc.concat(value); // eslint-disable-line no-param-reassign @@ -27,9 +20,9 @@ export const fromTreeToListExtended = ( treeIds: string[], treeXLevel = 0, treeYLevel = 0, - parentNode: (TableNode & ExtendedNode) | Nullish, -): (TableNode & ExtendedNode)[] => - (nodes || []).reduce((acc: (TableNode & ExtendedNode)[], value: TableNode) => { + parentNode: ExtendedNode | Nullish, +): ExtendedNode[] => + (nodes || []).reduce((acc: ExtendedNode[], value: TableNode) => { let listNode; if (value.nodes) { @@ -41,14 +34,17 @@ export const fromTreeToListExtended = ( const extendedNode = { treeXLevel, treeYLevel, - parentNode: parentNode || data, - ancestors: parentNode ? [parentNode, ...parentNode.ancestors] : [parentNode || data], - } as ExtendedNode; + // TODO: data needs to be explicitly typed here for this edge case of root + parentNode: (parentNode || data) as ExtendedNode, + ancestors: (parentNode + ? [parentNode, ...(parentNode?.ancestors ?? [])] + : [parentNode || data]) as ExtendedNode[], + }; listNode = { ...listNode, ...extendedNode, - } as TableNode & ExtendedNode; + } as ExtendedNode; acc = acc.concat(listNode); // eslint-disable-line no-param-reassign diff --git a/src/pagination/usePagination.ts b/src/pagination/usePagination.ts index 805e003..3caabfb 100644 --- a/src/pagination/usePagination.ts +++ b/src/pagination/usePagination.ts @@ -5,7 +5,7 @@ import { useSyncControlledState } from '@table-library/react-table-library/commo import { useSyncRefState } from '@table-library/react-table-library/common/util/useSyncRefState'; import { Action, State, StateAndChange } from '@table-library/react-table-library/types/common'; -import { Data, TableNode } from '@table-library/react-table-library/types/table'; +import { Data, ExtendedNode, TableNode } from '@table-library/react-table-library/types/table'; import { Pages, Pagination, @@ -139,7 +139,7 @@ const usePagination = ( getPageBoundaries, }; - const modifier = (nodes: TableNode[]): TableNode[] => { + const modifier = (nodes: TableNode[]): ExtendedNode[] => { if (mergedOptions.isServer) { return nodes; } diff --git a/src/sort/useSort.tsx b/src/sort/useSort.tsx index fd55815..50f389d 100644 --- a/src/sort/useSort.tsx +++ b/src/sort/useSort.tsx @@ -8,7 +8,7 @@ import IconChevronSingleUp from '@table-library/react-table-library/common/icons import IconChevronSingleUpDown from '@table-library/react-table-library/common/icons/IconChevronSingleUpDown'; import { Action, State, StateAndChange } from '@table-library/react-table-library/types/common'; -import { Data, TableNode } from '@table-library/react-table-library/types/table'; +import { Data, ExtendedNode, TableNode } from '@table-library/react-table-library/types/table'; import { Sort, SortOptions, @@ -163,7 +163,7 @@ const useSort = ( const stateAndGetters = { ...state, sortFn }; - const modifier = (nodes: TableNode[]): TableNode[] => { + const modifier = (nodes: TableNode[]): ExtendedNode[] => { if (mergedOptions.isServer) { return nodes; } diff --git a/src/tree/useTree.tsx b/src/tree/useTree.tsx index 31d69ca..5e75194 100644 --- a/src/tree/useTree.tsx +++ b/src/tree/useTree.tsx @@ -17,6 +17,7 @@ import { Features, FeatureProps, RowProps, + ExtendedNode, } from '@table-library/react-table-library/types/table'; import { Tree, @@ -120,7 +121,7 @@ const useTree = ( }, }; - const modifier = (nodes: TableNode[]): TableNode[] => { + const modifier = (nodes: TableNode[]): ExtendedNode[] => { if (mergedOptions.isServer) { return nodes; } diff --git a/src/types/common.ts b/src/types/common.ts index 15fd45b..aac93e8 100644 --- a/src/types/common.ts +++ b/src/types/common.ts @@ -1,3 +1,5 @@ +import { ExtendedNode, TableNode } from './table'; + export type Nullish = null | undefined; export type Action = { @@ -21,7 +23,7 @@ export type StateAndChange = { onChange?: MiddlewareFunction; }; -export type Modifier = (nodes: any[]) => any[]; +export type Modifier = (nodes: TableNode[]) => TableNode[] | ExtendedNode[]; type IdReducerFunctionsOptions = { isCarryForward?: boolean; diff --git a/src/types/table.ts b/src/types/table.ts index 1cd6df6..3c1cb45 100644 --- a/src/types/table.ts +++ b/src/types/table.ts @@ -85,6 +85,13 @@ export type TableNode = { [prop: string]: any; }; +export type ExtendedNode = T & { + treeXLevel?: number; + treeYLevel?: number; + parentNode?: ExtendedNode | Nullish; + ancestors?: ExtendedNode[]; +}; + export type Data = { pageInfo?: any; nodes: TableNode[];