-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
TypeScript 4.6 regression #48118
Comments
@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 @thetutlage 's code. |
This 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(); |
So shouldn't this be under breaking changes? |
Yes, that's why it's a Docs bug assigned to the blog post author |
Okay! Thanks for the clarification :) |
This 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.5
and4.6
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
I expect
this.contextAccumlator = context
to report zero errors.🙂 Expected behavior
Instead it says
The text was updated successfully, but these errors were encountered: