Skip to content

Different inference behavior between method and function syntax as opposed to arrow functions. #38623

@dragomirtitian

Description

@dragomirtitian

TypeScript Version: 4.0.0-dev.20200516

Search Terms: object literal inference arrow function method

Expected behavior: M in inferred as { foo: () => void; }

Actual behavior:: When using function or method syntax M is inferred as unknown. Using arrow functions works fine.

Related Issues: #32230 But that specific case seems fixed

Code

function make<M>(o: { mutations: M,  action: (m: M) => void }) { }

make({
   mutations: {
       foo () { } // Nope
       // foo: function () { } // Nope 
       // foo: () => { } // OK
   },
   action: (a) => { a.foo() }
})
Output
"use strict";
function make(o) { }
make({
    mutations: {
        // foo () { } // Nope
        // foo: function () { } // Nope 
        foo: () => { } // OK
    },
    action: (a) => { a.foo(); }
});
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions