Interpreter icon indicating copy to clipboard operation
Interpreter copied to clipboard

Allow struct types to typecheck optional/unknown fields

Open WillBAnders opened this issue 2 years ago • 4 comments

Currently, structs use structural subtyping which considers any struct to be a subtype so long as the required fields are present. This is insufficient for handling optional/unknown fields, which is an important part of type safety when working with structs (particularly when involving default values).

  • Optional fields may be provided, but if so must typecheck successfully. It is unclear if there's an effective way to represent this with null such that users are not confused between required, nullable fields, optional, non-nullable fields (as with JSON). Maybe optional is handled differently for subtyping but accessing fields returns Nullable versions (properly wrapped for optional, nullable) ?
  • Unknown fields are generally a concern for type safety, but could be useful for abstractions involving structs (such as transforming specific fields but leaving all others untouched). There are currently no plans to do any kind of more customizable [key: string] clauses as with TypeScript, just a general "all other fields".

WillBAnders avatar May 20 '23 13:05 WillBAnders