Skip to content

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

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

Closed
iKBAHT opened this issue Oct 31, 2019 · 2 comments · Fixed by #35173
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@iKBAHT
Copy link

iKBAHT commented Oct 31, 2019

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

@AnyhowStep
Copy link
Contributor

AnyhowStep commented Oct 31, 2019

Maybe somewhat related?
#14829

If non-inferential usage is implemented, this might be a perfect use case.


TS playground is unusable on Android (can't edit anything), so I can't test but what happens when getter and deps are switched?


[Edit]
Switching them gives the desired inference.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Nov 8, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.8.0 milestone Nov 8, 2019
@ahejlsberg ahejlsberg added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Nov 18, 2019
@ahejlsberg
Copy link
Member

This worked in 3.5 and was broken by #25937. I'm thinking it is possible to recognize cases where the new logic in #25937 can be circumvented because it will have no effect anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants