Skip to content

Feature Request: Instantiate Generic Functions within Conditional TypesΒ #61133

Open
@sinclairzx81

Description

@sinclairzx81

πŸ” Search Terms

Instantiation Expressions, Conditional Types

βœ… Viability Checklist

⭐ Suggestion

I am looking for a way to be able to instantiate a generic function / return type via conditional type.

// some method

declare function method<T>(value: T): T

// ... this is possible

type R0 = ReturnType<typeof method<number>> 

// ... but this isn't 

type Infer<Func extends <T>(value: T) => unknown, U> = ReturnType<Func<U>>

type R1 = Infer<typeof method, number>

Related Issue: #22617

πŸ“ƒ Motivating Example

I have written a small type-level parser for one of the libraries I manage. I have support for type parameterization.

Parameterized Type

const T = Syntax('number') 

const Vector = Syntax({ T }, `{ x: T, y: T, z: T }`)  // const T: TObject<{
                                                      //   x: TNumber,
                                                      //   y: TNumber,
                                                      //   z: TNumber
                                                      // }>

... but would also like to support generic functions as parameters (and resolve exterior types via function call / instantiation expression)

const Vector = <T extends TSchema>(T: T) => Type.Object({ x: T, y: T, z: T })

const T = Syntax({ Vector }, `Vector<number>`)        // const T: TObject<{
                                                      //    x: TNumber,
                                                      //    y: TNumber,
                                                      //    z: TNumber
                                                      // }>
                                      

When implementing the second case, I noticed it's not actually possible to instantiate the Vector signature within the context of a conditional type expression (i.e. the parser). In this case, I have a parsed TNumber type ready to supply the generic Vector function, but no way to instantiate it.

Essentially I would need the following but in a conditional type (Reference)

const Vector = <T extends TSchema>(T: T) => Type.Object({ x: T, y: T, z: T })

// ... internal to the parser

type ParsedNumber = TNumber 

type Instanced = ReturnType<typeof Vector<ParsedNumber>> // need this via conditional type

πŸ’» Use Cases

  1. What do you want to use this for?

Implement generic types in a type level syntax parser.

  1. What shortcomings exist with current approaches?

Cannot instantiate generic signatures within a conditional type.

  1. What workarounds are you using in the meantime?

I'm not sure there are workarounds.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions