Skip to content

in keyword can't be used on a narrowed down expression with a nullable generic constraintΒ #51501

Closed
@Andarist

Description

@Andarist

Bug Report

πŸ”Ž Search Terms

in keyword, narrow, nullable, NonNullable, generic constraint

πŸ•— Version & Regression Information

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

export function isHTMLTable<T extends object | null>(table: T): boolean {
  return !!table && 'html' in table; // Type 'NonNullable<T>' may represent a primitive value, which is not permitted as the right operand of the 'in' operator.(2638)
}

export function isHTMLTable2(table: object | null): boolean {
  return !!table && 'html' in table; // no error
}

πŸ™ Actual behavior

The generic version of this function is reporting an error on the in operator but the non-generic one isn't

πŸ™‚ Expected behavior

I believe that both of them should not error.

This was originally reported by @jcalz here

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions