Skip to content

TypeScript 4.6 regression #48118

@thetutlage

Description

@thetutlage

Bug Report

I have read the release notes and the code that is breaking for me is not mentioned in the breaking changes section.

🔎 Search Terms

None. As the code works with 4.5

🕗 Version & Regression Information

The typeof var === 'function' call narrows the variable to function vs one of the union.

  • This changed between versions 4.5 and 4.6

⏯ Playground Link

Playground link with relevant code

💻 Code

type Context = Record<string, any>

class Foo<T extends Context> {  
  private contextAccumlator!: () => T | Promise<T>

  constructor(
    context: T | (() => T | Promise<T>)
  ) {
    if (typeof context === 'function') {
      this.contextAccumlator = context
    }
  }
}

🙁 Actual behavior

I expect this.contextAccumlator = context to report zero errors.

🙂 Expected behavior

Instead it says

Type '(() => T | Promise<T>) | (T & Function)' is not assignable to type '() => T | Promise<T>'.
  Type 'T & Function' is not assignable to type '() => T | Promise<T>'.
    Type 'Context & Function' provides no match for the signature '(): T | Promise<T>'.(2322)

Metadata

Metadata

Labels

DocsThe issue relates to how you learn TypeScript

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions