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

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

Closed
Andarist opened this issue Nov 12, 2022 · 1 comment Β· Fixed by #51502
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@Andarist
Copy link
Contributor

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

@HolgerJeromin
Copy link
Contributor

Could/should this be cherry picked to 4.9 branch?
cc @RyanCavanaugh
makes removing away from suppressImplicitAnyIndexErrors a bit easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
5 participants