Skip to content

Negated {} and null | undefined predicates on unknown should still narrow #60789

Closed
@JoshuaKGoldberg

Description

@JoshuaKGoldberg

πŸ”Ž Search Terms

{} undefined null predicate unknown narrowing

πŸ•— Version & Regression Information

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

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXywGcA5HDY5CCIgCwAoA3KCZEALnjQGtUcB3VAEoOTFgiLwA3gF8A3AChQkWAhTpseAiUrVC9Uaw7deA4fAPjC8VDvgAfTqlCIsqEMAWLw0OPDB5CDHNmQ0ceflRPLER4OiJSch1aRhCQQUEpeXhgsQVsgHp8+AA9AH55aXl5aNiAQniyCipki3TM7Is8+EKS8srqmLjtZr0UsTbJLJzWLp6yiqqaunrh3X1UianOqbm+oA

πŸ’» Code

declare function isNotNullish(value: unknown): value is {};
declare function isNullish(value: unknown): value is null | undefined;

declare const value: unknown;

if (isNotNullish(value)) {
  value;
  // ^?
}

if (!isNotNullish(value)) {
  value;
  // ^?
}

if (isNullish(value)) {
  value;
  // ^?
}

if (!isNullish(value)) {
  value;
  // ^?
}

πŸ™ Actual behavior

value only narrows when the type predicate is called directly, not when negated with a !.

The narrowed types of value in that example are:

  1. isNotNullish(value): {}
  2. !isNotNullish(value): unknown ❌
  3. isNullish(value): null undefined
  4. !isNullish(value): unknown ❌

πŸ™‚ Expected behavior

The narrowed types of value should be:

  1. isNotNullish(value): {}
  2. !isNotNullish(value): null | undefined
  3. isNullish(value): null undefined
  4. !isNullish(value): {}

Additional information about the issue

No response

Activity

Andarist

Andarist commented on Dec 17, 2024

@Andarist
Contributor

Using unknown is so 2024: TS playground

JoshuaKGoldberg

JoshuaKGoldberg commented on Dec 18, 2024

@JoshuaKGoldberg
ContributorAuthor

What a ridiculously fast turnaround time. ❀️

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

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @JoshuaKGoldberg@Andarist@gabritto

      Issue actions

        Negated {} and null | undefined predicates on unknown should still narrow Β· Issue #60789 Β· microsoft/TypeScript