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

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

Closed
Asweallcan opened this issue Feb 28, 2024 · 5 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@Asweallcan
Copy link

🔎 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

@Andarist
Copy link
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) {},
  },
});

@Asweallcan
Copy link
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? @Andarist

@Andarist
Copy link
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!
  },
});

@Asweallcan
Copy link
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

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 28, 2024
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants