Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: This-TypingThe issue relates to providing types to thisThe issue relates to providing types to thisFixedA PR has been merged for this issueA PR has been merged for this issueHelp WantedYou can do thisYou can do this
Milestone
Description
Bug Report
When the following is true:
- You are passing an object created inline to a function
- The type of that function argument is generic
- The object has a getter that accesses
this - The object also has a function
Then the return type of the getter will be any.
If you either remove the function from the object or create the object before passing it into the function the getter will return the correct type.
🔎 Search Terms
any, object, generic
🕗 Version & Regression Information
- Bug present in all versions available on the playground, including nightly
⏯ Playground Link
Playground link with relevant code
💻 Code
declare function generic<T>(v: T): T;
// Passing object directly to genery gives type `any` on `get hi()`
const store = generic({
hello: "hi",
// return type of getter is any
get hi() {
return this.hello;
},
// If you remove this function, return type of getter will be string
doSomething() { }
});
const res = store.hi // any
// Passing object directly to genery gives type `any` on `get hi()`
const obj = {
hello: "hi",
// return type of getter is string
get hi() {
return this.hello;
},
doSomething() { }
};
const store2 = generic(obj)
const res2 = store2.hi // string🙁 Actual behavior
Return type of getters are any
🙂 Expected behavior
Return type of getters have the type of the returned data
Reacted by Jesse Niininen
Reacted by whzx5byb, Jesse Niininen, angel t., Matthew Endicott and λ/dragon
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: This-TypingThe issue relates to providing types to thisThe issue relates to providing types to thisFixedA PR has been merged for this issueA PR has been merged for this issueHelp WantedYou can do thisYou can do this
Activity
craigphicks commentedon Jun 14, 2022
Looks related -- from src/compiler/checker.ts,
resolveCallExpression(...)lqzhgood commentedon May 9, 2024
#58483
RyanCavanaugh commentedon Sep 9, 2026
This is fixed by PR #62243. With
--target es6 --strict, 4.7.2 and 6.0.0-dev.20251210 inferanyfor the inline getter, while 6.0.0-dev.20251211, TypeScript 6.0.3, and the current native compiler inferstringfor both the inline and stored-object getters.