Skip to content

Argument type should carry through to return value #16528

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
Arlen22 opened this issue Jun 14, 2017 · 3 comments
Closed

Argument type should carry through to return value #16528

Arlen22 opened this issue Jun 14, 2017 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@Arlen22
Copy link

Arlen22 commented Jun 14, 2017

I have a function that takes an input object, adds properties to it based on the arguments, and returns it. In this case, the argument is a string containing multiple key-value pairs, but if I know what those keys are, I can tell the type checker what type is going to be returned. But I don't feel like declaring the input type twice, since it be an object literal, as seen here.

declare function parseFields<S, T = {}>(text: string, result: T): typeof result & S;
//returns a type with two fields, not three
parseFields<{hello:string, world:string}>("hello=there&world=here", { hi: 'there' })

Instead of T = {}, I want to somehow tell it that it is supposed to infer it from result, yet still be able to specify S. I'm putting this in as a feature request, because I'm pretty sure there is no way to do this.

T = typeof result gives me the error that result cannot be found. Yet, I think that this might be the best way to imply this, although I don't know how that would fit into the overall flow. What about T = typeofarg result or T = typeof arg result?

@jcalz
Copy link
Contributor

jcalz commented Jun 14, 2017

Workaround: use currying?

declare function parseFields<S>(): <T>(text: string, result: T) => S & T;
// returns value of type { hello: string; world: string; } & { hi: string; }
parseFields<{ hello: string, world: string }>()("hello=there&world=here", { hi: 'there' });

@DanielRosenwasser DanielRosenwasser changed the title Argument type should care through to return value Argument type should carry through to return value Jun 19, 2017
@DanielRosenwasser
Copy link
Member

Looks like a duplicate of #16597 (sorry, I know this one was filed first but that's how it got triaged).

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Jun 19, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Aug 17, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Aug 17, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants