Skip to content

Generic inference different between equivalent function expression and arrow function expression in object literal #38845

Closed
@fsenart

Description

@fsenart

TypeScript Version: Nightly

Search Terms: parameter, inference, generic, function expression, arrow function expression

Expected behavior:

In function b, parameter a should be inferred as a: () => 42.

Actual behavior:

When using function expression instead of arrow function expression, parameter a is inferred as a: unknown.

Related Issues: #32230

Code

export declare function foo<A>(
    options: {
        a: A
        b: (a: A) => void;
    }
): void;

foo(
    {
        a: () => { return 42 },
        b(a) {}, // a: () => 42
    }    
);

foo(
    {
        a: function () { return 42 },
        b(a) {}, // a: unknown
    }    
);

foo(
    {
        a() { return 42 },
        b(a) {}, // a: unknown
    }    
);
Output
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
foo({
    a: () => { return 42; },
    b(a) { },
});
foo({
    a: function () { return 42; },
    b(a) { },
});
foo({
    a() { return 42; },
    b(a) { },
});
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixedFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions