Skip to content

Type narrowing not work with inferred function arguments #45671

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

Closed
weijarz opened this issue Sep 1, 2021 Β· 1 comment
Closed

Type narrowing not work with inferred function arguments #45671

weijarz opened this issue Sep 1, 2021 Β· 1 comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@weijarz
Copy link

weijarz commented Sep 1, 2021

Bug Report

πŸ”Ž Search Terms

type narrow argument

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.4.2#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwHcYoAHAMTTAApEAueKzAD3oGEMmBKeAXgD54ANxxZgnesNEAoUJFgIU6bHnhwAjsixwAyhhhUAzvQN6sqAObihI4FKlmMIGIihgE7JvADeU+PAwAFjA4BIYYUBjIRvCoyAC2AEZOVqgggk4A3FIAvnZQBgCe6EiUyviIAEyMHGwc3D5+ECAY8NEmMGbm8AA+SFAQBgg88GigiGYgwPD506gFvvBYiAwAhAbczAB0gcGhAIwADAecC+qaOnqGJ7lSRKQU6FT5RWAlSrj41SzwHvULTS02qYLD0+gMhiNUGMJlMZlA5gslqt1vAtjsQlRDsdTiANFoQLp9OscidbsRyJQnoViopMB8GMw-o1mq1jMCur0XODeJDoalYQZZvM-EiqGsNhxtkEMViTn4zvjCVd4AB6FXwAA8AFotfAAAYGPWLQXtTqgrmDElSIA

πŸ’» Code

declare function wrapFunc(f: (ctx: Ctx) => void): void
declare function requireStr(s: string): void

interface Ctx {
  throw(status: number): never;
}

async function f2(ctx: Ctx) {
  let s: string | false = undefined as any
  if (!s) ctx.throw(100)
  requireStr(s)
}

wrapFunc(async function (ctx: Ctx) {
  let s: string | false = undefined as any
  if (!s) ctx.throw(100)
  requireStr(s)
})

wrapFunc(async function (ctx) {
  let s: string | false = undefined as any
  if (!s) ctx.throw(100)
  requireStr(s) // <-- Compile Error!  here `s` is string | false
})
@andrewbranch andrewbranch added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Sep 2, 2021
@andrewbranch
Copy link
Member

Documented as a limitation of #32695. #33622 considered calling this out as an error, but ultimately decided against it for never-returning functions. You can choose between the type annotation and simply writing return ctx.throw(100).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants