-
Notifications
You must be signed in to change notification settings - Fork 12.8k
never rest type not assignable to complex rest type #33495
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
Comments
The goal here would be to make all signatures structurally assignable to |
@ahejlsberg this seems reasonable to me. Thoughts? |
I did some experiments in the playground. This seems to break only when the rest parameter is generic. Use a concrete type and all is well: Interestingly, |
This is where it fails in checking: const sourceCount = getParameterCount(source);
const sourceRestType = getNonArrayRestType(source);
const targetRestType = getNonArrayRestType(target);
if (sourceRestType && targetRestType && sourceCount !== targetCount) {
// We're not able to relate misaligned complex rest parameters
return Ternary.False;
} |
@jack-williams That doesn't seem to explain why it succeeds for the non-generic case. What is the exact definition of "complex" here? |
A rest type where you don't know the bounds, or whether it is unbounded---I would guess. I think |
FWIW. I think the fix is basically: function getNonArrayRestType(signature: Signature) {
const restType = getEffectiveRestType(signature);
return restType && !isArrayType(restType) && !isTypeAny(restType) && !isTypeNever(restType) ? restType : undefined;
}
|
Repro taken from TS' source code, TypeScript/src/compiler/core.ts Lines 1460 to 1464 in c447ebc
/**
* Safer version of `Function` which should not be called.
* Every function should be assignable to this, but this should not be assignable to every function.
*/
export type AnyFunction = (...args: never[]) => void;
function foo<T extends any[]>() {
const fn1: (...rest: T) => void = (..._) => {};
const fn2: AnyFunction = fn1 // error
}
But not every function is assignable to it |
TypeScript Version: 3.5.1
Search Terms:
Code
Expected behavior:
Assignment of
fn1
tofn2
should pass.Actual behavior:
Assignment of
fn1
tofn2
should fails.Playground Link: link
Related Issues:
#33457
The text was updated successfully, but these errors were encountered: