Home > Back-end >  Property 'selected' of type 'boolean' is not assignable to 'string' in
Property 'selected' of type 'boolean' is not assignable to 'string' in

Time:06-19

on this piece of code

export interface Field {
  selected:  boolean;
  value: any;
}

export interface ConflictingVersionModel {
  [key: string]: Field;
  selected: boolean;
}

I get this error: TS2411: Property 'selected' of type 'boolean' is not assignable to 'string' index type 'Field'.

the same one if I try:

export interface ConflictingVersionModel {
  [key: string]: {
    selected:  boolean;
    value: any;
  };
  selected: boolean;
}

Any ideas on what might be wrong?

CodePudding user response:

Yeah that won't work. An index signature means 'all properties of this object have this type'.

You can however achieve what it looks like you are trying to do using an intersection type

export interface Field {
  selected:  boolean;
  value: any;
}

type ConflictingVersionModel = {
  [key: string]: Field;
} & { selected: boolean }
  • Related