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

Arrow functions treated differently than functions and methods #29520

Description

TypeScript Version: 3.2.2 and 3.3.0-dev.20190119

Search Terms: arrow function method object literal

Code

type Test<K extends keyof any> = {
  [P in K | "foo"]: P extends "foo" ? true : () => any
}

function test<T extends Test<keyof T>>(arg: T) {
    return arg;
}

const res1 = test({
  foo: true,
  bar() {
    // Notice the type param of `test` is inferred as `Test<never>`
  }
})

const res2 = test({
  foo: true,
  bar: function () {
    // Same error here.
  }
})

const res3 = test({
  foo: true,
  bar: () => {
    // This is fine.
  }
})

Expected behavior: The first two test calls should infer the given object type correctly.

Actual behavior: They instead infer the object type as Test<never>. 😢

Playground Link: Click here

Related Issues: #17867 (sort of)

Activity

  1. aleclarson commented on Jan 21, 2019

    @aleclarson
    Author

    Interestingly, the following works:

    const res4 = test({
      foo: true,
      bar,
    })
    function bar() {
      // This is fine.
    }
  2. 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 TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions