Gourav Pokharkar
Gourav Pokharkar

Reputation: 1648

null is not assignable to void when flag --strictNullChecks is enabled

As per the TypeScript Documentation:

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.

But, when I tried following code snippets:

// This worked correctly
let voidIsUndefined: void = undefined;

// While following code thrown a compilation error "Type 'null' is not assignable to type 'void'."
let voidIsNull: void = null;

It did not work completely as expected.

As per the docs, I should be able to assign null to void type. Can someone please clear this doubt?

Upvotes: 2

Views: 717

Answers (1)

Shaun Luttin
Shaun Luttin

Reputation: 141662

The TypeScript docs are misleading and you're correct to feel confused. There is an issue about this here: https://github.com/microsoft/TypeScript-Handbook/issues/468

Upvotes: 1

Related Questions