skip to Main Content

I have interface

interface ITest {
    [key: string]: {
        [key: string]: ITest;
        access: number[];
    };
}

But I get error

Property ‘access’ of type ‘number[]’ is not assignable to ‘string’ index type ‘ITest’.

2

Answers


  1. It seems like TypeScript is getting confused because you’ve set up a generic index signature [key: string] for the outer object in your ITest interface. However, within that object, TypeScript is expecting a property named 'access' with a type of number[]. This creates a conflict because the inner object should conform to the ITest type according to your index signature.

    To resolve this, you can make your code clearer by defining a separate interface for the inner structure. Let’s call it IInnerStructure:

    interface IInnerStructure {
      [key: string]: ITest;
      access: number[];
    }
    
    interface ITest {
      [key: string]: IInnerStructure;
    }
    

    Now, in the ITest interface, the inner objects follow the structure defined by IInnerStructure. This way, TypeScript won’t have issues with the index signature conflicting with the explicit access property.

    Login or Signup to reply.
  2. Here issue with your ITest interface. Declare another interface so that you can use it in both the ITest interface and also for the access property.

    interface ITestEntrys {
        [key: string]: ITest;
        access: number[];
    }
    
    interface ITest {
        [key: string]: ITestEntrys;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search