I would like to create a type that is essentially
interface Type<T> {
state: boolean;
[id: string]: <T>;
};
where the id
objects are optional, i.e. the type only requires the state
field. However, this gives the error Property 'isFetching' of type 'boolean' is not assignable to 'string' index type
.
After some reading, I have attempted to split up and intersect the two type separately as
interface TypeA {
[id: string]: SomeOtherType;
}
interface TypeB extends Partial<TypeA> {
state: boolean;
};
which gives me Property 'state' of type 'boolean' is not assignable to 'SomeOtherType' index type
I can make the typescript error-free by splitting everything up like
type TypeA = {
[key: string]: SomeType;
}
type TypeB = {
state: boolean;
}
type TypeC = Partial<TypeA> & TypeB
but then trying to use TypeC
with just the state
property I get
const c: TypeC = {
state: false
}
Type '{ state: false; }' is not assignable to type 'SomeType'. Type '{ state: false; }' is not assignable to type 'Partial<TypeA>'. Property 'state' is incompatible with index signature. Type 'false' is not assignable to type 'SomeType | undefined'
So it seems to forgets about my state
Type and prioritizes my index string type, which I don't want it to do. How can I fix this?! I am at a loss