Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined and void #8937

Open
Conaclos opened this issue Nov 6, 2022 · 0 comments
Open

Undefined and void #8937

Conaclos opened this issue Nov 6, 2022 · 0 comments

Comments

@Conaclos
Copy link

Conaclos commented Nov 6, 2022

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.

@Conaclos Conaclos changed the title Distinguish undefined from void Undefined and void Nov 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant