Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
JoshuaKGoldberg opened this issue Dec 17, 2024 · 2 comments Β· Fixed by #60795
Closed

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

JoshuaKGoldberg opened this issue Dec 17, 2024 · 2 comments Β· Fixed by #60795
Labels
Bug A bug in TypeScript Help Wanted You can do this

Comments

@JoshuaKGoldberg
Copy link
Contributor

πŸ”Ž 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

@Andarist
Copy link
Contributor

Using unknown is so 2024: TS playground

@gabritto gabritto added Bug A bug in TypeScript Help Wanted You can do this labels Dec 18, 2024
@JoshuaKGoldberg
Copy link
Contributor Author

What a ridiculously fast turnaround time. ❀️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants