Skip to content

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

Closed
microsoft/TypeScript-Handbook
#1053

Description

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.

Activity

  1. mhegazy commented on May 25, 2017

    @mhegazy
    Contributor

    do not use void as a type for a variable. use null | undefined if that s what you have in mind. void only makes sense in the return type of a function, to signify the lack of a value. and void is not an alias for undefined, it is a different type.

  2. Gozala commented on May 25, 2017

    @Gozala
    Author

    do not use void as a type for a variable. use null | undefined if that s what you have in mind.

    I don't it's more like if I want to represent Maybe / Option type I need to do a | null | undefined | void & I was hoping void | a would be what ?a is in flow.

    void only makes sense in the return type of a function, to signify the lack of a value. and void is not an alias for undefined, it is a different type.

    Interesting! Where can I learn about how void is different from undefined ?

    Never the less, I wanted to point out that behavior and linked documentation seems to be inconsistent. If implemented behavior makes more sense that documented behavior that's fine, but in that case maybe docs should be get updated to avoid misleading newcomers like me ;)

  3. RyanCavanaugh commented on May 30, 2017

    @RyanCavanaugh
    Member

    Not sure why the docs say that (maybe there was some confusion with function return types). Should edit to fix.

  4. DLiblik commented on Feb 3, 2018

    @DLiblik

    I find this issue crops up in this (highly contrived) situation (nodejs) - in real world situations there are sometimes promise functions that don't error but don't do anything in certain circumstances and so a direct resolve makes sense. Is there a way to do this?

    function someFunc():Promise<void> {
        // Can't do this because resolve expects a value
        return Promise.resolve();
        // Can't do either of these or you get the error mentioned:
        return Promise.resolve(null);
        return Promise.resolve(undefined);
    }
  5. karlhorky commented on Jun 24, 2019

    @karlhorky
    Contributor

    Not sure why the docs say that (maybe there was some confusion with function return types). Should edit to fix.

    Ryan Cavanaugh (@RyanCavanaugh) I would do a PR to fix this. Can't find where in the repo I should do this though.

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

    Similar issue here: #11758, would probably also close that issue.

  6. karlhorky commented on Jun 24, 2019

    @karlhorky
    Contributor
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

      Development

      No branches or pull requests

      Issue actions