Skip to content

Proposal: Consider introducing a shorthand syntax for nullable types #18430

Closed
@MartinHaeusler

Description

@MartinHaeusler

Static analysis on "nullable" variables is a good thing. However, using this feature also requires to explicitly create union types with null for every variable and member that can in fact be null. So, for example to create a nullable (and undefineable) number member...

interface MyInterface {
  x: number | null | undefined
}

It is currently possible to use a shortcut for undefined:

interface MyInterface {
   x?: number | null
}

Note that the two interface definitions above are equivalent, but | null is always required.

However, for my taste, this is all way too verbose for something as common as a nullable type. I would like to propose the following:

interface MyInterface {
   x: number?
}

... where number? is the shorthand for number | null | undefined. This little tweak would save a lot of typing and improve general acceptance of strict null checking. This isn't a new idea. Languages like Ceylon use the same method and syntax.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions