demergent-labs / azle

A WebAssembly runtime for TypeScript and JavaScript on ICP
MIT License
201 stars 36 forks source link

Consider supporting `?` optional types #621

Open dansteren opened 2 years ago

dansteren commented 2 years ago

Consider the following record type in azle:

type HttpRequest = {
  url: string,
  body: Opt<blob>
}

This isn't the solution that my brain naturally reaches for. Instead, I would much rather say

type HttpRequest = {
  url: string,
  body?: blob
}

The former requires you to instantiate a request like this:

const myRequest = { url: "https://example.com", body: null}

whereas the latter lets you do

const myRequest = { url: "https://example.com" }

We are using the Opt<T> type because it is an algebraic datatype that is more similar to Rust/Motoko. We should look into supporting the ? optional properties because I think that will be more intuitive to TS devs.

lastmjs commented 2 years ago

Consider that optional properties result in undefined and not null. The algebraic Option data type can only be one of two explicit properties, Some or None. We're currently representing this as a value or null. It may not be appropriate to also introduce the concept of undefined and implicit Option types, which is a foreign concept in Rust (and possibly Motoko, not sure).