From 9466c868be628a59bc37437451996d0478be0822 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Wed, 4 Jan 2023 21:38:43 -0500 Subject: [PATCH] Tighten types --- packages/react-json-tree/src/ItemRange.tsx | 2 +- packages/react-json-tree/src/JSONArrayNode.tsx | 6 +++--- .../react-json-tree/src/JSONIterableNode.tsx | 2 +- .../react-json-tree/src/JSONNestedNode.tsx | 8 ++++---- packages/react-json-tree/src/JSONNode.tsx | 2 +- .../react-json-tree/src/JSONObjectNode.tsx | 4 ++-- packages/react-json-tree/src/JSONValueNode.tsx | 4 ++-- .../src/getCollectionEntries.ts | 10 +++++----- packages/react-json-tree/src/index.tsx | 2 +- packages/react-json-tree/src/types.ts | 18 +++++++++--------- 10 files changed, 29 insertions(+), 29 deletions(-) diff --git a/packages/react-json-tree/src/ItemRange.tsx b/packages/react-json-tree/src/ItemRange.tsx index b35b5390..52abad4a 100644 --- a/packages/react-json-tree/src/ItemRange.tsx +++ b/packages/react-json-tree/src/ItemRange.tsx @@ -3,7 +3,7 @@ import JSONArrow from './JSONArrow'; import type { CircularCache, CommonInternalProps } from './types'; interface Props extends CommonInternalProps { - data: any; + data: unknown; nodeType: string; from: number; to: number; diff --git a/packages/react-json-tree/src/JSONArrayNode.tsx b/packages/react-json-tree/src/JSONArrayNode.tsx index bd73d643..76da807c 100644 --- a/packages/react-json-tree/src/JSONArrayNode.tsx +++ b/packages/react-json-tree/src/JSONArrayNode.tsx @@ -4,14 +4,14 @@ import type { CommonInternalProps } from './types'; // Returns the "n Items" string for this node, // generating and caching it if it hasn't been created yet. -function createItemString(data: any) { +function createItemString(data: unknown) { return `${(data as unknown[]).length} ${ (data as unknown[]).length !== 1 ? 'items' : 'item' }`; } interface Props extends CommonInternalProps { - data: any; + data: unknown; nodeType: string; } @@ -24,7 +24,7 @@ export default function JSONArrayNode({ data, ...props }: Props) { nodeType="Array" nodeTypeIndicator="[]" createItemString={createItemString} - expandable={data.length > 0} + expandable={(data as unknown[]).length > 0} /> ); } diff --git a/packages/react-json-tree/src/JSONIterableNode.tsx b/packages/react-json-tree/src/JSONIterableNode.tsx index ca8dc454..0357b511 100644 --- a/packages/react-json-tree/src/JSONIterableNode.tsx +++ b/packages/react-json-tree/src/JSONIterableNode.tsx @@ -23,7 +23,7 @@ function createItemString(data: any, limit: number) { } interface Props extends CommonInternalProps { - data: any; + data: unknown; nodeType: string; } diff --git a/packages/react-json-tree/src/JSONNestedNode.tsx b/packages/react-json-tree/src/JSONNestedNode.tsx index d83bf4cf..0e5b5be5 100644 --- a/packages/react-json-tree/src/JSONNestedNode.tsx +++ b/packages/react-json-tree/src/JSONNestedNode.tsx @@ -10,7 +10,7 @@ import type { CircularCache, CommonInternalProps } from './types'; */ export interface RenderChildNodesProps extends CommonInternalProps { - data: any; + data: unknown; nodeType: string; circularCache: CircularCache; level: number; @@ -23,7 +23,7 @@ interface Range { interface Entry { key: string | number; - value: any; + value: unknown; } function isRange(rangeOrEntry: Range | Entry): rangeOrEntry is Range { @@ -87,10 +87,10 @@ function renderChildNodes( } interface Props extends CommonInternalProps { - data: any; + data: unknown; nodeType: string; nodeTypeIndicator: string; - createItemString: (data: any, collectionLimit: number) => string; + createItemString: (data: unknown, collectionLimit: number) => string; expandable: boolean; } diff --git a/packages/react-json-tree/src/JSONNode.tsx b/packages/react-json-tree/src/JSONNode.tsx index a0292d66..f7703350 100644 --- a/packages/react-json-tree/src/JSONNode.tsx +++ b/packages/react-json-tree/src/JSONNode.tsx @@ -7,7 +7,7 @@ import JSONValueNode from './JSONValueNode'; import type { CommonInternalProps } from './types'; interface Props extends CommonInternalProps { - value: any; + value: unknown; } export default function JSONNode({ diff --git a/packages/react-json-tree/src/JSONObjectNode.tsx b/packages/react-json-tree/src/JSONObjectNode.tsx index 211ef7ea..95401f83 100644 --- a/packages/react-json-tree/src/JSONObjectNode.tsx +++ b/packages/react-json-tree/src/JSONObjectNode.tsx @@ -4,13 +4,13 @@ import type { CommonInternalProps } from './types'; // Returns the "n Items" string for this node, // generating and caching it if it hasn't been created yet. -function createItemString(data: any) { +function createItemString(data: unknown) { const len = Object.getOwnPropertyNames(data).length; return `${len} ${len !== 1 ? 'keys' : 'key'}`; } interface Props extends CommonInternalProps { - data: any; + data: unknown; nodeType: string; } diff --git a/packages/react-json-tree/src/JSONValueNode.tsx b/packages/react-json-tree/src/JSONValueNode.tsx index d25132a3..24e4272d 100644 --- a/packages/react-json-tree/src/JSONValueNode.tsx +++ b/packages/react-json-tree/src/JSONValueNode.tsx @@ -19,9 +19,9 @@ interface Props { labelRenderer: LabelRenderer; nodeType: string; styling: Styling; - value: any; + value: unknown; valueRenderer: ValueRenderer; - valueGetter?: (value: any) => any; + valueGetter?: (value: any) => unknown; } export default function JSONValueNode({ diff --git a/packages/react-json-tree/src/getCollectionEntries.ts b/packages/react-json-tree/src/getCollectionEntries.ts index eed85aeb..a08c0859 100644 --- a/packages/react-json-tree/src/getCollectionEntries.ts +++ b/packages/react-json-tree/src/getCollectionEntries.ts @@ -1,6 +1,6 @@ import type { SortObjectKeys } from './types'; -function getLength(type: string, collection: any) { +function getLength(type: string, collection: unknown) { if (type === 'Object') { // eslint-disable-next-line @typescript-eslint/ban-types return Object.keys(collection as {}).length; @@ -11,8 +11,8 @@ function getLength(type: string, collection: any) { return Infinity; } -function isIterableMap(collection: any) { - return typeof (collection as Map).set === 'function'; +function isIterableMap(collection: unknown) { + return typeof (collection as Map).set === 'function'; } function getEntries( @@ -21,7 +21,7 @@ function getEntries( sortObjectKeys: SortObjectKeys, from = 0, to = Infinity -): { entries: { key: string | number; value: any }[]; hasMore?: boolean } { +): { entries: { key: string | number; value: unknown }[]; hasMore?: boolean } { let res; if (type === 'Object') { @@ -97,7 +97,7 @@ function getRanges(from: number, to: number, limit: number) { export default function getCollectionEntries( type: string, - collection: any, + collection: unknown, sortObjectKeys: SortObjectKeys, limit: number, from = 0, diff --git a/packages/react-json-tree/src/index.tsx b/packages/react-json-tree/src/index.tsx index a732c37d..5aa394a5 100644 --- a/packages/react-json-tree/src/index.tsx +++ b/packages/react-json-tree/src/index.tsx @@ -17,7 +17,7 @@ import type { } from './types'; interface Props extends Partial { - data: any; + data: unknown; theme?: Theme; invertTheme?: boolean; } diff --git a/packages/react-json-tree/src/types.ts b/packages/react-json-tree/src/types.ts index 412af62e..6a67f376 100644 --- a/packages/react-json-tree/src/types.ts +++ b/packages/react-json-tree/src/types.ts @@ -3,11 +3,11 @@ import { StylingFunction } from 'react-base16-styling'; export type Key = string | number; -export type KeyPath = (string | number)[]; +export type KeyPath = readonly (string | number)[]; export type GetItemString = ( nodeType: string, - data: any, + data: unknown, itemType: React.ReactNode, itemString: string, keyPath: KeyPath @@ -21,26 +21,26 @@ export type LabelRenderer = ( ) => React.ReactNode; export type ValueRenderer = ( - valueAsString: any, - value: any, + valueAsString: unknown, + value: unknown, ...keyPath: KeyPath ) => React.ReactNode; export type ShouldExpandNodeInitially = ( keyPath: KeyPath, - data: any, + data: unknown, level: number ) => boolean; -export type PostprocessValue = (value: any) => any; +export type PostprocessValue = (value: unknown) => unknown; -export type IsCustomNode = (value: any) => boolean; +export type IsCustomNode = (value: unknown) => boolean; -export type SortObjectKeys = ((a: any, b: any) => number) | boolean; +export type SortObjectKeys = ((a: unknown, b: unknown) => number) | boolean; export type Styling = StylingFunction; -export type CircularCache = any[]; +export type CircularCache = unknown[]; export interface CommonExternalProps { keyPath: KeyPath;