1

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

Hannah
  • 43
  • 3
  • What happens when one of your objects has `state` as its id? – kaya3 Oct 08 '21 at 14:06
  • I'm inclined to close this as a duplicate of https://stackoverflow.com/questions/61431397/how-to-define-typescript-type-as-a-dictionary-of-strings-but-with-one-numeric-i but as it stands this doesn't seem to be a [mre] of your issue, since I would expect an error in ... ugh you edited that didn't you right while I was typing. Okay, never mind that last bit – jcalz Oct 08 '21 at 14:08
  • 1
    Sorry, I was trying to make my post more clear...I am trying to get better at my StackOverflow posting – Hannah Oct 08 '21 at 14:14
  • Please see [this question](https://stackoverflow.com/questions/61431397/how-to-define-typescript-type-as-a-dictionary-of-strings-but-with-one-numeric-i) and [its answer](https://stackoverflow.com/a/61434547/2887218). Good luck! – jcalz Oct 08 '21 at 14:16

0 Answers0