flow icon indicating copy to clipboard operation
flow copied to clipboard

Undefined and void

Open Conaclos opened this issue 1 year ago • 0 comments

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)
  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.

Conaclos avatar Nov 06 '22 22:11 Conaclos