-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Generic inference different between equivalent function expression and arrow function expression in object literal #38845
Comments
Just to add another data-point to the strangeness, if foo(
{
a: () => 42,
b: (a) => {}, // a: () => 42
}
)
foo(
{
a: () => 42,
b: function(a) {}, // a: () => 42
}
) so this suggests that the inference problem also has to do with how |
While experimenting around this problem, I found out another strange behavior that I suspect to be related to this one: export declare function foo<C, A>(options: {
c: C;
a: (c: C) => A;
b: (b: A) => void;
}): void;
// a(): no parameter -> return type can be inferred
foo({
c: 42,
a: () => {}, // a: (c: number) => void
b(b) {}, // b: (b: void) => void
});
// a(): a parameter (with inferred type) -> return type cannot be inferred
foo({
c: 42,
a: (c) => {}, // a: (c: number) => unknown
b(b) {}, // b: (b: unknown) => void
});
// a(): a parameter (with explicit type) -> return type can be inferred
foo({
c: 42,
a: (c:number) => {}, // a: (c: number) => void
b(b) {}, // b: (b: void) => void
}); However, if function export declare function foo<C, A>(options: {
c: C;
a: (c: C) => A;
}): void;
// a(): no parameter -> return type can be inferred
foo({
c: 42,
a: () => {}, // a: (c: number) => void
});
// a(): a parameter -> return type can be inferred
foo({
c: 42,
a: (c) => {}, // a: (c: number) => void
}); |
@ahejlsberg I tried to reason about the context-sensitiveness of this and failed. Can you weigh in, or is this just a bug? |
This is a design limitation. Similar to #38872. A arrow function with no parameters is not context sensitive, but a function expression with no parameters is context sensitive because of the implicit |
TypeScript Version: Nightly
Search Terms: parameter, inference, generic, function expression, arrow function expression
Expected behavior:
In function
b
, parametera
should be inferred asa: () => 42
.Actual behavior:
When using function expression instead of arrow function expression, parameter
a
is inferred asa: unknown
.Related Issues: #32230
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: