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

Generic cannot be narrowed to satisfy NonNullable #48691

Closed
longzheng opened this issue Apr 14, 2022 · 2 comments Β· Fixed by #49119
Closed

Generic cannot be narrowed to satisfy NonNullable #48691

longzheng opened this issue Apr 14, 2022 · 2 comments Β· Fixed by #49119
Labels
Duplicate An existing issue was already created Fix Available A PR has been opened for this issue

Comments

@longzheng
Copy link

longzheng commented Apr 14, 2022

Bug Report

πŸ”Ž Search Terms

  • generic can't be narrowed
  • narrow nonnullable generic

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried from 4.6.2 to 4.0.5 and nightly v4.7.0-dev.20220408

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function test<T extends string | null>(input: T): NonNullable<T> {
  if (!input) { // this should narrow `input` to `T extends string`
    throw new Error();
  }

  return input; // type error
}

πŸ™ Actual behavior

Type error

Type 'T' is not assignable to type 'NonNullable<T>'.
  Type 'string | null' is not assignable to type 'NonNullable<T>'.
    Type 'null' is not assignable to type 'NonNullable<T>'.

πŸ™‚ Expected behavior

TypeScript should know I've narrowed the generic type to satisfied the NonNullable type.

I came across #43183 which I was expecting to also address this scenario.

@longzheng
Copy link
Author

This might be a duplicate of #48048

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 20, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants