1

I would like to describe my object using type. My object is recursive.

const value = {
  name: 'one';
  someKey: {
     name: 'two'
  };
}

const valu2 = {
  name: 'one'; 
}


For my type I am getting an error:

type Foo = {
  name: string;
  [key: string]: Foo;
}
Property 'name' of type 'string' is not assignable to string index type 'Foo'.

What is wrong? I would like to use type not interface.

vimov32802
  • 89
  • 2
  • 5
  • It's not the recursion that's the problem. It's that you have an inconsistent string index signature. Please see [this question](https://stackoverflow.com/questions/61431397/how-to-define-typescript-type-as-a-dictionary-of-strings-but-with-one-numeric-i). – jcalz Apr 09 '21 at 18:19
  • If I translate the code from the answer to that question to your example, I get [this](https://tsplay.dev/WKkr8W), which goes through the different workarounds. I strongly recommend refactoring to `RefactoredFoo` since the compiler will make working with that much easier than all the craziness required to deal with unsupported "rest index signature" workarounds. – jcalz Apr 09 '21 at 18:27
  • Does this suffice as an answer to your question? If so I will likely close this as a duplicate; if not, please edit your question to highlight the differences and why that answer doesn't apply. Good luck! – jcalz Apr 09 '21 at 18:28

0 Answers0