portabletext/react-portabletext

Typescript errors

aorsten opened this issue · 2 comments

I'm building a project with Sanity, Typescript, ViteJS and React, and wanted to incorporate the PortableText component.

Unfortunately, when running tsc I get errors from this package, or more specifically from the toolkit package. See the errors below.
Seems like this is due to my tsconfig including this setting: "noUncheckedIndexedAccess": true,

I would expect to be able to use this package with that setting enabled. I think the fix must be employed in the toolkit package. Could you enable the tsconfig setting noUncheckedIndexedAccess in toolkit and then in this repo?

node_modules/@portabletext/react/src/toolkit/buildMarksTree.ts:37:7 - error TS2532: Object is possibly 'undefined'.

37       lastNode.children.push({...span, _type: '@span', children: [], markType: '<unknown>'})
         ~~~~~~~~

node_modules/@portabletext/react/src/toolkit/buildMarksTree.ts:46:22 - error TS2532: Object is possibly 'undefined'.

46         const mark = nodeStack[pos].markKey
                        ~~~~~~~~~~~~~~

node_modules/@portabletext/react/src/toolkit/buildMarksTree.ts:67:15 - error TS2532: Object is possibly 'undefined'.

67         _key: span._key,
                 ~~~~

node_modules/@portabletext/react/src/toolkit/buildMarksTree.ts:82:28 - error TS2345: Argument of type 'PortableTextSpan | ArbitraryTypedObject | undefined' is not assignable to parameter of type 'PortableTextSpan | TypedObject'.
  Type 'undefined' is not assignable to type 'PortableTextSpan | TypedObject'.

82     if (isPortableTextSpan(span)) {
                              ~~~~

node_modules/@portabletext/react/src/toolkit/buildMarksTree.ts:91:58 - error TS2769: No overload matches this call.
  Overload 1 of 2, '(...items: ConcatArray<ToolkitTextNode | ToolkitNestedPortableTextSpan<MarkDefinition> | ArbitraryTypedObject>[]): (ToolkitTextNode | ... 1 more ... | ArbitraryTypedObject)[]', gave the following error.
    Argument of type 'ArbitraryTypedObject | undefined' is not assignable to parameter of type 'ConcatArray<ToolkitTextNode | ToolkitNestedPortableTextSpan<MarkDefinition> | ArbitraryTypedObject>'.
      Type 'undefined' is not assignable to type 'ConcatArray<ToolkitTextNode | ToolkitNestedPortableTextSpan<MarkDefinition> | ArbitraryTypedObject>'.
  Overload 2 of 2, '(...items: (ToolkitTextNode | ToolkitNestedPortableTextSpan<MarkDefinition> | ArbitraryTypedObject | ConcatArray<...>)[]): (ToolkitTextNode | ... 1 more ... | ArbitraryTypedObject)[]', gave the following error.
    Argument of type 'ArbitraryTypedObject | undefined' is not assignable to parameter of type 'ToolkitTextNode | ToolkitNestedPortableTextSpan<MarkDefinition> | ArbitraryTypedObject | ConcatArray<...>'.
      Type 'undefined' is not assignable to type 'ToolkitTextNode | ToolkitNestedPortableTextSpan<MarkDefinition> | ArbitraryTypedObject | ConcatArray<...>'.

91       currentNode.children = currentNode.children.concat(span)
                                                            ~~~~


node_modules/@portabletext/react/src/toolkit/buildMarksTree.ts:122:43 - error TS2532: Object is possibly 'undefined'.

122     occurences[mark] = occurences[mark] ? occurences[mark] + 1 : 1
                                              ~~~~~~~~~~~~~~~~

node_modules/@portabletext/react/src/toolkit/buildMarksTree.ts:128:28 - error TS2345: Argument of type 'PortableTextSpan | TypedObject | undefined' is not assignable to parameter of type 'PortableTextSpan | TypedObject'.
  Type 'undefined' is not assignable to type 'PortableTextSpan | TypedObject'.

128         isPortableTextSpan(sibling) &&
                               ~~~~~~~

node_modules/@portabletext/react/src/toolkit/buildMarksTree.ts:171:9 - error TS2532: Object is possibly 'undefined'.

171     if (node._type === '@span' && node.children) {
            ~~~~

node_modules/@portabletext/react/src/toolkit/buildMarksTree.ts:171:35 - error TS2532: Object is possibly 'undefined'.

171     if (node._type === '@span' && node.children) {
                                      ~~~~

node_modules/@portabletext/react/src/toolkit/nestLists.ts:23:26 - error TS2345: Argument of type 'T | undefined' is not assignable to parameter of type 'TypedObject | PortableTextBlock<MarkDefinition, PortableTextSpan | ArbitraryTypedObject>'.
  Type 'undefined' is not assignable to type 'TypedObject | PortableTextBlock<MarkDefinition, PortableTextSpan | ArbitraryTypedObject>'.

23     if (!isListItemBlock(block)) {
                            ~~~~~

node_modules/@portabletext/react/src/toolkit/nestLists.ts:24:17 - error TS2345: Argument of type 'T | undefined' is not assignable to parameter of type 'NestListsOutputNode<T>'.
  Type 'undefined' is not assignable to type 'NestListsOutputNode<T>'.

24       tree.push(block)
                   ~~~~~

node_modules/@portabletext/react/src/toolkit/nestLists.ts:79:38 - error TS2345: Argument of type 'NestListsOutputNode<T> | undefined' is not assignable to parameter of type 'TypedObject | PortableTextBlock<MarkDefinition, PortableTextSpan | ArbitraryTypedObject>'.
  Type 'undefined' is not assignable to type 'TypedObject | PortableTextBlock<MarkDefinition, PortableTextSpan | ArbitraryTypedObject>'.

79       const match = findListMatching(tree[tree.length - 1], block)
                                        ~~~~~~~~~~~~~~~~~~~~~

node_modules/@portabletext/react/src/toolkit/nestLists.ts:94:38 - error TS2345: Argument of type 'NestListsOutputNode<T> | undefined' is not assignable to parameter of type 'TypedObject | PortableTextBlock<MarkDefinition, PortableTextSpan | ArbitraryTypedObject>'.

94       const match = findListMatching(tree[tree.length - 1], {level: block.level || 1})

Thanks for raising! With your PR to the toolkit and a new release of it, this should now be fixed in v1.0.3