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’.
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
It seems like TypeScript is getting confused because you’ve set up a generic index signature
[key: string]
for the outer object in yourITest
interface. However, within that object, TypeScript is expecting a property named'access'
with a type ofnumber[]
. This creates a conflict because the inner object should conform to theITest
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
:Now, in the
ITest
interface, the inner objects follow the structure defined byIInnerStructure
. This way, TypeScript won’t have issues with the index signature conflicting with the explicitaccess
property.Here issue with your
ITest
interface. Declare another interface so that you can use it in both theITest
interface and also for theaccess
property.