3

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; }
1
  • Retrun a KeyValuePair<int,string> so you do not need to create a class. It looks like you need a method with three overrides 1) KeyValuePair<int,string> GetType(int id, string label) 2) KeyValuePair<int,string> GetType(int id, string label, bool IsExcluded) 3) KeyValuePair<int,string> GetType(int id, string label, bool IsExcluded, string taxonomy) Commented Mar 30, 2018 at 10:15

1 Answer 1

2
  & { 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 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.