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?

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

    Development

    No branches or pull requests

    Issue actions