Skip to content

Documentation misleading on void encompassing null? #11758

Closed
microsoft/TypeScript-Handbook
#1053
@aseemk

Description

@aseemk

TypeScript Version: 2.0.3

Code

With --strictNullChecks:

let x: string | void = 'hello';
let y: string | void = undefined;
let z: string | void = null;

Expected behavior: compiles, since the documentation states:

However, when using the --strictNullChecks flag, null and undefined are only assignable to void and their respective types.

https://www.typescriptlang.org/docs/handbook/basic-types.html#null-and-undefined

Actual behavior: the last line fails to compile:

[eval].ts (3,1): Type 'null' is not assignable to type 'string | void'. (2322)

Rationale: is there a less verbose way, with --strictNullChecks, to specify a type like string | null | undefined? i.e. Is there a type that covers both null and undefined?

Activity

mhegazy

mhegazy commented on Oct 20, 2016

@mhegazy
Contributor

is there a less verbose way, with --strictNullChecks, to specify a type like string | null | undefined? i.e. Is there a type that covers both null and undefined?

no. We could not find a way to do that that is not confusing to some users. see #7426.

if you really want to define something like this, i would recomend adding

type Optional<T> = T | null | undefined;
added
DocsThe issue relates to how you learn TypeScript
on Oct 20, 2016
aseemk

aseemk commented on Oct 20, 2016

@aseemk
Author

Thanks for the quick answer and link!

So consider this issue just an issue to clarify the documentation then.

donaldpipowitch

donaldpipowitch commented on Mar 29, 2017

@donaldpipowitch
Contributor

Just stumbled over this. I always thought that void would be a union type of null and undefined and wondered why this throws with --strictNullChecks:

interface Foo {
  bar: void;
}

// Type 'null' is not assignable to type 'void'. 
const f: Foo = {
  bar: null
};

So what is void exactly for --strictNullChecks? Just an alias to undefined?

if you really want to define something like this, i would recommend adding

type Optional<T> = T | null | undefined;

Any idea to add something like this to the lib.*.d.ts files? Or as:

type Optional<T> = {
    [P in keyof T]?: T[P] | null | undefined;
};

Or a real common union type for null and undefined? type voidlike = null | undefined;?

aluanhaddad

aluanhaddad commented on Mar 29, 2017

@aluanhaddad
Contributor

I think that an issue here is that while technically JavaScript doesn't have any notion of void functions, having void in the type system is actually useful catching certain logic errors.

That said, returning null from a void function absolutely needs to be a type error. Returning undefined and returning without a value result in the same runtime behavior. The caller gets undefined.

KaelWD

KaelWD commented on Dec 20, 2018

@KaelWD

Just an alias to undefined?
Returning undefined and returning without a value result in the same runtime behavior

Yet void cannot be assigned to undefined

declare function returnsUndefined(): undefined
declare function returnsVoid(): void

const nothing: void = returnsUndefined() // works
const nothing2: undefined = returnsVoid() // Type 'void' is not assignable to type 'undefined'.

Looks like there's more on this over at #20006

karlhorky

karlhorky commented on Jun 24, 2019

@karlhorky
Contributor

Just found a similar issue #16075, will do a PR to fix the docs.

Edit: PR open: microsoft/TypeScript-Handbook#1053

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsThe issue relates to how you learn TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @aseemk@donaldpipowitch@karlhorky@aluanhaddad@mhegazy

      Issue actions

        Documentation misleading on void encompassing null? · Issue #11758 · microsoft/TypeScript