Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript to corresponding C# syntax

I have the following Type in typescript

export type Excludable<T extends FilterValue> = T & { isExcluded?: boolean }

where FilterValue:

export type FilterValue = {
   id: number,
   label: string,
}

type T could be the following:

export type AreaFilterValue = FilterValue & {
  type: "Area",
  taxonomy?: LocationTaxonomy,
}

Can I somehow write the above in C#?

ideally I would need something like:

 public class Excludable<T> : T where T : FilterValue
 {
        bool? IsExcluded { get; set; }

 }

but it shows an error of "Could not derive from T cause it's a type parameter"

What I am trying to achieve is to have an object like:

  1. { id, label }
  2. { id, label, isExcluded }
  3. { id, label, isExcluded, type, taxonomy }

Edit: I currently have set in C# that FilterValue has the isExcluded property

Ideally I would like to use it in my code like follows:

public Excludable<AreaFilterValue>[] OpenAreas { get; set; }
like image 421
Hakuna Matata Avatar asked Jan 19 '26 14:01

Hakuna Matata


1 Answers

  & { isExcluded?: boolean }

Is Bassicaly an anonymous Extended interface:

public interface FilterValueExt : FilterValue {
   bool? IsExcluded { get; set; }
}

You cant make generic subclasses in c# so for every use of Excludable You'd have to create a new interface or use Excludable as an interface and implement on any class that uses this method:

public interface Excludable
  bool? IsExcluded { get; set; }
}

pulic class AreaFilterValue : FilterValue, Excludable  {
   // implementation here +  type: "Area",   taxonomy?: LocationTaxonomy,
}

i'dd suggest you do the same in typescript:

interface Excludable{
    isExcluded?: boolean
}

var t= {isExcluded: true||false||undefined}; // typescript knows this is/canbe a excludable 
like image 189
Joel Harkes Avatar answered Jan 22 '26 05:01

Joel Harkes