Skip to content

TS doesn't narrow generic parameter T to null, despite an explicit comparisonΒ #57803

Open
@arafathusayn

Description

@arafathusayn

πŸ”Ž Search Terms

T to null

πŸ•— Version & Regression Information

  • This is a type narrowing issue
  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.5.0-pr-57465-110#code/GYVwdgxgLglg9mABDAzgORAG0wHgCoB8AFAG4CGmIApgFyJ4CUiA3gFAD07i3P3AegH5W3GMESkK1RAF5ZiMFkxM2vcpSrdO9ADSIUACzhYAJogBGGhdmGIAvqxsAnKlBCOkaqbOnzFAblZ7IA

πŸ’» Code

function isNull<T>(value: T) {
  if (value === null) {
    value  // T, should be null
  }

  return value === null;
}

πŸ™ Actual behavior

value is T inside the if block of value === null (explicit comparison)

πŸ™‚ Expected behavior

value should be of type null inside the if block of value === null

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Possible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions