Repository navigation
generics can only detect arrow function in object but not function expression #57572
Description
Activity
Andarist commented
on Feb 28, 2024 ContributorMore actionsAn object method without an explicit
thisparameter is going to always be context-sensitive (it has that impliedthisparameter). Your issue falls under this umbrella issue: #47599You can verify this by annotating
thisexplicitly in yourbbb:type C = <Methods, Attached extends (methods: Methods) => void>(options: { methods: Methods; attached: Attached; }) => any; var Component: C = () => {}; Component({ attached(methods) { methods.bbb(); // ok }, methods: { bbb(this: unknown) {}, }, });
An object method without an explicit
thisparameter is going to always be context-sensitive (it has that impliedthisparameter). Your issue falls under this umbrella issue: #47599You can verify this by annotating
thisexplicitly in yourbbb:type C = <Methods, Attached extends (methods: Methods) => void>(options: { methods: Methods; attached: Attached; }) => any; var Component: C = () => {}; Component({ attached(methods) { methods.bbb(); // ok }, methods: { bbb(this: unknown) {}, }, });
what if I need to use Type C to define bbb's "this" type,will this issue be solved or not? Mateusz Burzyński (@Andarist)
Andarist commented
on Feb 28, 2024 ContributorMore actionsYou can even workaround this particular situation by reordering your
attachedandmethods.attachedacts as a consumer here and it's better to put the consumer after the producer (methodshere):type C = <Methods, Attached extends (methods: Methods) => void>(options: { methods: Methods; attached: Attached; }) => any; var Component: C = () => {}; Component({ methods: { bbb() {}, // no `this` annotation! }, attached(methods) { methods.bbb(); // ok, yay! }, });
You can even workaround this particular situation by reordering your
attachedandmethods.attachedacts as a consumer here and it's better to put the consumer after the producer (methodshere):type C = <Methods, Attached extends (methods: Methods) => void>(options: { methods: Methods; attached: Attached; }) => any; var Component: C = () => {}; Component({ methods: { bbb() {}, // no `this` annotation! }, attached(methods) { methods.bbb(); // ok, yay! }, });
ohhhhhhhh, I will try this, thanks
- addedDuplicateAn existing issue was already createdAn existing issue was already created
on Feb 28, 2024 typescript-bot commented
on Mar 2, 2024 ContributorMore actionsThis issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
- locked as resolved and limited conversation to collaborators
on Oct 22, 2025
🔎 Search Terms
typescript
generics
arrow function
function
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play?#code/C4TwDgpgBAwlC8UA8AoK6oFkLABYHsATAZwBo0MBBYYAQwGNcJCEoAKAWxwJIC4tuRYgEoEAPigA3fAEtCKMWwrp8YYDPwA7YvwDeyjOi54h-bCZIBuA4do0GTQv2p1Gza4YC+KUfAm1NEGsUSVoAJ1h8DjAtCE1gfjhENl8JXShvFBgomM044DZ9W3s3Qk5BElEiwwxjHmIAOgAjFpSoAHp2qDww-AB3buIoCDDesINPckM60yhqmqgWprb07y8UT2EULJzY+MKDO1dHcosROZtaisalts6ofABrCamrs71L9CX+Nr85jImG2EQA
💻 Code
🙁 Actual behavior
Parameter "methods" in "attach" function should be functions declared in "methods" property, but it works only when function is arrow function.
🙂 Expected behavior
Parameter "methods" in "attach" function should be functions declared in "methods" property
Additional information about the issue
No response