Skip to content

generics can only detect arrow function in object but not function expression #57572

Description

@Asweallcan

🔎 Search Terms

typescript
generics
arrow function
function

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAwlC8UA8AoK6oFkLABYHsATAZwBo0MBBYYAQwGNcJCEoAKAWxwJIC4tuRYgEoEAPigA3fAEtCKMWwrp8YYDPwA7YvwDeyjOi54h-bCZIBuA4do0GTQv2p1Gza4YC+KUfAm1NEGsUSVoAJ1h8DjAtCE1gfjhENl8JXShvFBgomM044DZ9W3s3Qk5BElEiwwxjHmIAOgAjFpSoAHp2qDww-AB3buIoCDDesINPckM60yhqmqgWprb07y8UT2EULJzY+MKDO1dHcosROZtaisalts6ofABrCamrs71L9CX+Nr85jImG2EQA

💻 Code

type C = <
        Methods,
        Attached = (methods: Methods) => void
    >(
        options: {
            methods: Methods;
            attached: Attached;
        }
    ) => any;
    
    var Component: C = () => { }
    
    Component({
        attached(methods) {
            methods.bbb() // throw ts error
        },
        methods: {
            bbb() { }
        }
    })
    
    Component({
        attached(methods) {
            methods.bbb() // ok
        },
        methods: {
            bbb: () => { }
        }
    })

🙁 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

Activity

  1. Andarist commented on Feb 28, 2024

    @Andarist
    Contributor

    An object method without an explicit this parameter is going to always be context-sensitive (it has that implied this parameter). Your issue falls under this umbrella issue: #47599

    You can verify this by annotating this explicitly in your bbb:

    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) {},
      },
    });
  2. Asweallcan commented on Feb 28, 2024

    @Asweallcan
    Author

    An object method without an explicit this parameter is going to always be context-sensitive (it has that implied this parameter). Your issue falls under this umbrella issue: #47599

    You can verify this by annotating this explicitly in your bbb:

    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)

  3. Andarist commented on Feb 28, 2024

    @Andarist
    Contributor

    You can even workaround this particular situation by reordering your attached and methods. attached acts as a consumer here and it's better to put the consumer after the producer (methods here):

    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!
      },
    });
  4. Asweallcan commented on Feb 28, 2024

    @Asweallcan
    Author

    You can even workaround this particular situation by reordering your attached and methods. attached acts as a consumer here and it's better to put the consumer after the producer (methods here):

    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

  5. typescript-bot commented on Mar 2, 2024

    @typescript-bot
    Contributor

    This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions