Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'selected' of type 'boolean' is not assignable to 'string' index type 'Field'

Tags:

typescript

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?

like image 681
shalamel_bartalogonos Avatar asked Oct 24 '25 18:10

shalamel_bartalogonos


1 Answers

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 }
like image 189
Ben Wainwright Avatar answered Oct 27 '25 18:10

Ben Wainwright



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!