8000 Argument type should carry through to return value · Issue #16528 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content
Argument type should carry through to return value #16528
Closed
@Arlen22

Description

@Arlen22

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?

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

      0