Repository navigation
TypeScript 4.6 regression #48118
Description
Activity
- added a commit that references this issue
on Mar 4, 2022 Harminder Virk (@thetutlage) I played around with this a bit and reduced your example:
function isFunction<T extends Function>(it: unknown): it is T { return typeof it === 'function' } function isFn(it: unknown): it is Function { return typeof it === 'function' } function f<T>(arg: T | (() => T)) { let x: () => T if (typeof arg === 'function') x = arg // (error) arg: (() => T) | (T & Function) if (isFunction(arg)) x = arg // (works) arg: () => T if (isFn(arg)) x = arg // (works) arg: () => T }
This can be reproduced as far back as version 3.3.3 (play).
🤔 Did you mean there is no regression?
Weirdly enough, it seems those are two different cases. This issue's sample code was indeed working by 4.5.5, broken in 4.6.2, and still broken in 4.7.0-dev.20220304. While the second example was never fixed as far as I can tell.
Maybe a recent change caused the old bug to surface in Harminder Virk (@thetutlage) 's code.
RyanCavanaugh commented
on Mar 4, 2022 MemberMore actionsThis is a correct error that was not properly found before:
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 } } invoke() { this.contextAccumlator(); } } function isPromise(arg: any): arg is Promise<any> { return false; } const p = new Foo<(n: string) => void>(n => n.substring(0)); // Crashes p.invoke();
- addedDocsThe issue relates to how you learn TypeScriptThe issue relates to how you learn TypeScript
on Mar 4, 2022 So shouldn't this be under breaking changes?
RyanCavanaugh commented
on Mar 5, 2022 MemberMore actionsYes, that's why it's a Docs bug assigned to the blog post author
Okay! Thanks for the clarification :)
Reacted by Ryan CavanaughThis was "fixed" in #49119; should it have been?
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.4.5and4.6⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
I expect
this.contextAccumlator = contextto report zero errors.🙂 Expected behavior
Instead it says