Skip to content

null is not assignable to void when strictNullChecks are enabled  #16075

Closed
microsoft/TypeScript-Handbook
#1053
@Gozala

Description

@Gozala

According to the documentation Null and Undefined are assignable to void.

However, when using the --strictNullChecks flag, null and undefined are only assignable to void and their respective types. This helps avoid many common errors. In cases where you want to pass in either a string or null or undefined, you can use the union type string | null | undefined. Once again, more on union types later on.

Bun in my experience only undefined is actually assignable to void:

TypeScript Version: 2.3

Code

let thing: void = null; // <- Type 'null' is not assignable to type 'void'.

Expected behavior:

I would expect null to be assignable to void. It makes for void to be just a union type void = null | undefined otherwise it's just an alias for undefined.

Actual behavior:

Only undefined is assignable to void.

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