镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Closed

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

Activity

  1. AnyhowStep commented on Oct 31, 2019

    @AnyhowStep
    Contributor

    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.

  2. added
    BugA bug in TypeScript
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Nov 18, 2019
  3. ahejlsberg commented on Nov 18, 2019

    @ahejlsberg
    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.

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

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