Skip to content

Generic inference different between equivalent function expression and shorthand method in object literal (vol 2) #34849

Closed
@iKBAHT

Description

@iKBAHT

TypeScript Version: 3.6.3 - 3.8.0-dev.20191031

Search Terms: parameter, inference, order, generic, function expression, function declaration

Code

interface IData {
  bar: boolean
}

declare function test<TDependencies>(
  getter: (deps: TDependencies, data: IData) => any,
  deps: TDependencies,
): any 

const DEPS = {
  foo: 1
}

// negative test
test(
  (deps, data) => ({
    fn1: function() { return deps.foo },
    fn2: data.bar
  }),
  DEPS
);

// positive tests
test(
  (deps: typeof DEPS, data) => ({
    fn1: function() { return deps.foo },
    fn2: data.bar
  }),
  DEPS
);

test(
  (deps, data) => ({
    fn1: () => deps.foo,
    fn2: data.bar
  }),
  DEPS
);

test(
  (deps, data) => {
    return {
      fn1() { return deps.foo },
      fn2: data.bar
    }
  },
  DEPS
);

test(
  (deps) => ({
    fn1() { return deps.foo },
    fn2: 1
  }),
  DEPS
);

Expected behavior: deps should be inferred as typeof DEPS in negative test

Actual behavior: deps inferred as unknown

Playground Link: link
Related Issues: I guess, the fix of #32230 bug led to the appearance of this bug

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix 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