Skip to content

Type narrowing not work with inferred function argumentsΒ #45671

@weijarz

Description

@weijarz

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
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions