Skip to content
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

Arrow functions treated differently than functions and methods #29520

Closed
aleclarson opened this issue Jan 21, 2019 · 1 comment · Fixed by #29647
Closed

Arrow functions treated differently than functions and methods #29520

aleclarson opened this issue Jan 21, 2019 · 1 comment · Fixed by #29647
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@aleclarson
Copy link

aleclarson commented Jan 21, 2019

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)

@aleclarson
Copy link
Author

Interestingly, the following works:

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

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

Successfully merging a pull request may close this issue.

3 participants