Skip to content

Undefined and void #8937

Open
Open

Description

@Conaclos

Hi!

Proposal

Support the literal type undefined and distinguish undefined from void.

The proposal may be divided into two proposals:

  1. add undefined type (backward compatible) (done)
  2. make void type the absence of value (breaking change)

Add undefined type

const x: undefined = undefined // ✔️

function f (x: undefined): void {}
f() // ❌ parameter is required
f(undefined) // ✔️

function g (): undefined { /* ❌ return is required */ } 
function h (): undefined { return /* ❌ a value must be returned */ } 
function j (): undefined { return undefined /* ✔️ */ } 

make void type the absence of value

const x: void // ❌ void cannot be used as variable type

function f (x: number | void): void {}
function f (x?: number): void {} // equivalent
f() // ✔️
f(undefined) // ❌

function g (): void { return undefined /* ❌ */ } 
function g (): void { return /* ✔️ */ } 

Use case / Rationales

This makes Flow more compatible with TypeScript.

This allows to type a function with a required parameter that accepts undefined:

function g (x: number | undefined) {}
f() // ❌ parameter is required
f(undefined) // ✔️

This makes the intent more clear: void is often associated with the absence of value. The use of void to denote undefined is misleading.

Activity

  1. changed the title [-]Distinguish undefined from void[/-] [+]Undefined and void[/+] on Nov 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions