0

I want to implement structure like this:

interface IDataProps {
  [key: string]: ReactNode | ((props: IRenderDataProps) => JSX.Element) // any key except those below
  key: string | number; // reserved key
  children?: IDataProps[] // reserved key
}

But i have an error:

TS2411: Property 'children' of type 'IDataProps[]' is not assignable to 'string' index type 'ReactNode | ((props: IRenderDataProps) => JSX.Element)'

What I'm trying to do:

type TDataSource = {
  [key: string]: ReactNode | ((props: IRenderDataProps) => JSX.Element); // any key except those below
} & {
  key: string | number; // reserved key
  children?: TDataSource[]; // reserved key
};

This option is good, but in deep nesting (level 2 of nesting or more) I get an error:

TS2322: Type '{ key: number; name: () => JSX.Element; children: { key: number; name: () => JSX.Element; }[]; }' is not assignable to type 'TDataSource'.

Type '{ key: number; name: () => JSX.Element; children: { key: number; name: () => JSX.Element; }[]; }' is not assignable to type '{ [key: string]: ReactNode | ((props: IRenderDataProps) => Element); }'.

Property 'children' is incompatible with index signature.

Additional clarification: for example, I have 2 reserved properties: key, children (optional recursive), and I want to have ANY other property that should be ReactNode | ((props: IRenderDataProps) => JSX.Element) if it is neither key nor children

const nestingExample: TDataSource[] = [
  {
    key: 5, // I'm required and reserved key
    [`problemIsThatImCustomKeyThatShouldBe-ReactNode | ((props: IRenderDataProps)`]:
      () => <>SomeJSX</>,
    children: [
      {
        key: 6,
        iAmAnotherKey: 'key'
      }
    ],
  },
];
Marty
  • 534
  • 8
  • 24
  • @TobiasS. No. This is not helps!! My key in `[key: string]: ReactNode | ((props: IRenderDataProps) => JSX.Element)`, can by ANY string except those keys: `key: string | number; // reserved key` and `children?: IDataProps[] // reserved key` – Marty Aug 06 '22 at 15:12
  • This is not currently possible in TypeScript; see [microsoft/TypeScript#17867](https://github.com/microsoft/TypeScript/issues/17867) and the answers to the linked questions for more information. – jcalz Aug 06 '22 at 15:44

0 Answers0