-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
No implicit any error on call in JS but not in TS #55062
Comments
Do you see same behavior if you declare all types and overloads in TS, and only try use it in JS (instead of JS docs declaration)? May be related to #39509 |
Type declarations: type ButNot<T, U> = T & { [K in Exclude<keyof U, keyof T>]?: never };
declare function foo<T>(context?: ButNot<T, Function>): void;
declare function foo<T>(mapFn: (index: number) => any, context?: T): void; In JS: // No errors β
// v- β (parameter) k: any
foo((k) => [k]);
// No errors β
// v- β
(parameter) k: number
foo((k) => [k], undefined); In TS: // No errors β
// v- β
(parameter) k: number
foo((k) => [k]);
// No errors β
// v- β
(parameter) k: number
foo((k) => [k], undefined); |
Experimenting with 3 overload signatures shows that only the first fails to get the type parameter. I'm not sure what's going on but at least I'm pretty sure it's not parsing. Here's the variant I ended up with: /**
* @template T
* @overload
* @param {(index: number) => any} mapFn
* @param {T} [context]
* @returns {void}
*
* @overload
* @param {Partial<T>} [bipartite]
* @returns {void}
*
* @overload
* @param {ButNot<T, Function>} [admissible]
* @returns {void}
*/
function foo(admitation) {} |
Confusingly, type baselines for this test case are correct. But the call still has the error from the OP: // @checkJs: true
// @allowJs: true
// @strict: true
// @noEmit: true
// @filename: /overloadTag4.js
/**
* @template T
* @template U
* @typedef {T & { [K in Exclude<keyof U, keyof T>]?: never }} ButNot
*/
/**
* @template T
* @overload
* @param {ButNot<T, Function>} [admissible]
* @returns {void}
* @overload
* @param {(index: number) => any} mapFn
* @param {T} [context]
* @returns {void}
*
* @param {any} admitation
*/
function foo(admitation) { !!admitation }
foo(x => [x]) |
I tested signature help some more, and it behaves the same in JS and TS--the first signature is instantiated and the others are not. Confusing but probably intentional. Quick info is not good at specifying which signature was selected in a call, but it turns out both TS and JS select the same signature: the mapFn one. Both fail to make any inferences since function getDefaultTypeArgumentType(isInJavaScriptFile: boolean): Type {
return isInJavaScriptFile ? anyType : unknownType;
} The inferred
|
Thank you @sandersn for getting to the bottom of this issue. |
In TS, T=unknown, so there's no implicit any error on In both cases, the same overload is chosen and there are no inference sources for T, so you're getting the default type parameter type for JS (any) vs TS (unknown). |
Well this is confusing because I observed TS inferring |
Poking at it again, I see that T=any causes the first overload with |
I see, so if I understood correctly:
Which seams to holdup: // v- β (parameter) k: any
foo<any>((k) => [k]);
// v- β
(parameter) k: number
foo<unknown>((k) => [k]); Thank you for taking the time to explain it to me π |
Bug Report
π Search Terms
js, overload
π Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ entries but non apply.
β― Playground Link
JS version playground link with relevant code
TS version playground link with relevant code
π» Code
JS version:
TS version:
ButNot
is as @RyanCavanaugh suggested in #4196 (comment).π Actual behavior
Parameter 'k' implicitly has an 'any' type.
error in JS version but not TS one.π Expected behavior
No errors.
PS: This feels like a bug to me but I'm not sure if there's another way to achieve the expected outcome.
The text was updated successfully, but these errors were encountered: