-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
'A type predicate's type must be assignable to its parameter's type' should not fire when in a generic predicate function #54116
Comments
Pretty sure this ends up as a duplicate of #33912. |
I don't think it's a dupe of #33912. That one's about relating the |
@fatcerberus Maybe. My thought was that the type assignability error appears because the conditional type is not resolved. |
The error message accurately describes what's going on and I think the motivation for that error is pretty straightforward. the only thing you need to do to this code is add this intersection: val is T & ExtractOr which shouldn't meaningfully change the behavior of the function at all |
@RyanCavanaugh Unfortunetly that does not work as intended and does meaningfully change the behvaviour. Here is a playground link type ExtractOr<T, U, Fallback = never> = T extends U ? T : Fallback;
export function isFunction<T>(val: T): val is T & ExtractOr<T, Function, (...args: unknown[]) => unknown> {
return typeof val === "function";
}
function foo1(x: unknown) {
if (isFunction(x)) {
type Y = typeof x;
// x should be narrowed to `(...args: unknown[]) => unknown`;
}
}
function foo2(x: number | undefined | ((val: string) => boolean)) {
if (isFunction(x)) {
type Y = typeof x;
const b: boolean = x(""); // <--- error here, x("") returns unknown
// x should be narrowed to `(val: string) => boolean`;
}
} |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@RyanCavanaugh why is this closed? I have shown why your "solution" doesn't work. |
Hello! π Maybe it's too late, but here is the solution (playground link) type Func<TParams extends readonly any[] = any[], TReturn = unknown> = (...args: TParams) => TReturn;
type ExtractOr<T, U, Fallback> = Extract<T, U> extends never ? (Fallback extends T ? Fallback : never) : Extract<T, U>
export function isFunction<T>(val: T): val is ExtractOr<T, Func, Func<unknown[]>> {
return typeof val === "function";
} The original problem comes from the posible situation, when some given So you need to handle this situation: ensure that |
ah thanks, will take a look |
Bug Report
π Search Terms
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Should be a valid user defined predicate which can narrow union types to just those variants that are functions, otherwise (specifically if
val
isunknown
)For example:
π Expected behavior
The TSC reports error 2677.
The text was updated successfully, but these errors were encountered: