Skip to content

Inferred never values don't properly type guardΒ #60185

Closed
@a-non-a-mouse

Description

@a-non-a-mouse

πŸ”Ž Search Terms

never type guard

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about type guarding

⏯ Playground Link

https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&moduleResolution=99&target=99&module=199&exactOptionalPropertyTypes=true&ts=5.6.3#code/GYVwdgxgLglg9mABAExgUwBQEoBcixoBuaATogN4BQiNiJaUIJSYIANm4gIYDO+RpANyUAvpUqhIsBIhhhgpACLpsFarXqNmKFVmFjKyNBDZd6iCAh5READzzWScgOaIAPvnZthlGMEQYAIS2WGq0svJKuvritoKIAPQJdrJ81jAciAAOcDw8MABGbACenhzifgHBoVThqJh6orHxSSlgcADuFlxgAOQ2BWhlbEA

πŸ’» Code

function die(): never {
    return null as never;
}

function inferDie() {
    return die();
}

declare const x: string | null;

if (!x) {
    inferDie();
}

x; // x is still possibly null

if (!x) {
    die();
}

x; // x now can't be null

πŸ™ Actual behavior

Two functions that have "identical" signatures, () => never, are not acting the same

πŸ™‚ Expected behavior

I'd expect both of these to act the same way since, for everything I can see, TS sees their signatures as being identical.

Additional information about the issue

No response

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