Skip to content

Null checking does not follow through functions: Object possibly 'null' or 'undefined' #18244

Closed

Description

TypeScript Version: 2.4.2

Code

function hasValueGeneric<T>(value: T): boolean {
    return value !== null && value !== undefined;
}

function hasValueAny(value: any): boolean {
    return value !== null && value !== undefined;
}

function test (value: string|null|undefined) {
    if (hasValueGeneric(value)) {
        return value[0]; // currently throws error but shouldn't
    }
    if (hasValueAny(value)) {
        return value[0]; // currently throws error but shouldn't
    }
    if (value !== null && value !== undefined) {
        return value[0]; // does not throw error, as expected
    }
}

tsconfig.json

{
    "compilerOptions": {
        "strictNullChecks": true
    }
}

Expected behavior:
Should follow through the function and compile.

Actual behavior:
Is not able to follow the function to the null check logic. Throws the below errors
(12,9): error TS2533: Object is possibly 'null' or 'undefined'.
(15,9): error TS2533: Object is possibly 'null' or 'undefined'.

Pretty sure this a bug and not a feature request, as it does not match standard user expectations based on reasonable extrapolation of documentation and isn't documented as known gap anywhere that I've found.

More than willing to submit a PR if I can get guidance on where and how to go about fixing. Not sure whether that is practical or not based on likely level of complexity for a fix. I have written in TypeScript full time for over a year now but have never contributed to the project before.

Activity

  1. ikatyang commented on Sep 5, 2017

    @ikatyang
    Contributor

    TS won't know if there's a type guard in your function unless you defined it, and auto type-guard generation looks like duplicate of #15265.

    -function hasValueGeneric<T>(value: T): boolean {
    +function hasValueGeneric<T>(value: T): value is {} {
        return value !== null && value !== undefined;
    }
    
    -function hasValueAny(value: any): boolean {
    +function hasValueAny(value: any): value is {} {
        return value !== null && value !== undefined;
    }
  2. beattyml1 commented on Sep 5, 2017

    @beattyml1
    Author

    That fixed my problem but when I looked in the docs before logging the issue I didn't see anything like this would you have any objection to having this added to the docs? If not I take I would submit a PR to this repo: https://github.com/Microsoft/TypeScript-Handbook correct?

  3. beattyml1 commented on Sep 5, 2017

    @beattyml1
    Author

    Actually looks like it is in the guide I was just in a sub section. I'm just going to close this since it seems to be not an issue with code or documentation. Sorry for taking up your time with a non-issue and thanks for the great response :)

  4. locked and limited conversation to collaborators on Jun 14, 2018
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions