typeshare icon indicating copy to clipboard operation
typeshare copied to clipboard

Hand made Option type

Open czocher opened this issue 2 years ago • 0 comments

We currently support Option<T> which can be represented as undefined or null in TypeScript. We also support Option<Option<T>> which lets one represent a field that can either be null or undefined (i.e. double optional pattern https://docs.rs/serde_with/latest/serde_with/rust/double_option/).

I'm wondering if there's any way we can create our own representation of double optional as:

enum Maybe<T> {
  Null,
  Undefined,
  Value(T)
}

Such that it will have the following TypeScript representation:

struct Something {
    field: Maybe<u32>,
}
interface Something {
    field?: number | null;
}

Without using Option<Option<T>>. Is this possible?

czocher avatar Oct 19 '23 11:10 czocher