Skip to content

Type inference does not work when in return position #34929

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

Closed
jerguslejko opened this issue Nov 5, 2019 · 3 comments
Closed

Type inference does not work when in return position #34929

jerguslejko opened this issue Nov 5, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@jerguslejko
Copy link

jerguslejko commented Nov 5, 2019

TypeScript Version: 3.7-beta

Search Terms: inference, generic functions

Code

export function foo<T, K>(props: {
    seed: T;

    producer: (x: T) => K;

    consumer: (data: K) => void;
}) {
    //
}

foo({
    seed: "foo",

    producer: (seed) => {
        // type of `seed` is string
        return seed; 
    },

    consumer: (data) => {
        // type of `data` is unknown 
    }
})

Expected behavior: TypeScript infers K to a string, so the type of data is string.

Actual behavior: data variable is unknown.

@AnyhowStep
Copy link
Contributor

I believe this is also because TS cannot perform inference on the same object multiple times?
#34865

It is why I use the builder pattern.
The builder pattern lets me break apart inference on one giant object into multiple inferences on multiple smaller objects/values
#34907

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 6, 2019
@typescript-bot
Copy link
Collaborator

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

@jerguslejko
Copy link
Author

@AnyhowStep is took me 2 months exactly to understand what you mean :))

I'm able to reproduce this behaviour by swapping from f({ a, b }) to f(a, b). The non-object version works correctly. However, I'm trying to type a TSX component which always receives an object.

I think I'm out of luck :(

Thank you though! I learned something

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