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

Feature Request: Instantiate Generic Functions within Conditional Types #61133

Open
6 tasks done
sinclairzx81 opened this issue Feb 6, 2025 · 4 comments
Open
6 tasks done
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@sinclairzx81
Copy link

🔍 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.

@sinclairzx81 sinclairzx81 changed the title Instantiate Generic Functions within Conditional Types Feature Request: Instantiate Generic Functions within Conditional Types Feb 6, 2025
@jcalz
Copy link
Contributor

jcalz commented Feb 6, 2025

#40542, #47607

I think I remember that extending instantiation expressions to the type level would give an ambiguous meaning to a type of the form F<T>. Like, what if you have type F<T=X> = <U>(u: U, t:T) => [T, U] or something? I’m sure that can be addressed but it’s messy.

#47607 (comment)

@sinclairzx81
Copy link
Author

@jcalz Hi, thanks for the reference links

Hmmm, it does seem this feature has been lingering for several years now. Reading up, it looks like the main challenges were a combination of design limitations (as it was in 2018) and a syntax differentiation issue, finding a way to make an appropriate distinction between "type instantiation" and "instantiation expression" within the context of conditional types.

It would be great to get an update though.

@RyanCavanaugh
Copy link
Member

See also #40179

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Feb 7, 2025
@sinclairzx81
Copy link
Author

sinclairzx81 commented Feb 12, 2025

Update: I've since found an alternative solution that solves for the motivating example

Solution supported for TS 4.8.4 and above (Reference).

// Generic Arguments
const T0 = Type.Argument(0)
const T1 = Type.Argument(1)
const T2 = Type.Argument(2)

// Generic Type with Parameters
const Vector = Syntax({ T0, T1, T2 },  `{
  x: T0,
  y: T1,
  z: T2
}`)

// Syntax Instantiated Generic Types (Solution Here)
const BasisVectors = Syntax({ Vector }, `{
  readonly type: 'BasisVectors'
  x: Vector<1, 0, 0>,
  y: Vector<0, 1, 0>,
  z: Vector<0, 0, 1>,
}`)

type BasisVectors = Static<typeof BasisVectors>

// type BasisVectors = {
//   readonly type: "BasisVectors";
//   x: { x: 1; y: 0; z: 0; };
//   y: { x: 0; y: 1; z: 0; };
//   z: { x: 0; y: 0; z: 1; };
// }

Would still be keen for the requested feature. For future reference, here is an updated (and simpler) case where the ConditionalReturnType type cannot be reasonably implemented (TS 5.8.0)

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

// ...

type A = ReturnType<typeof method<number>>               // type A: number

type ConditionalReturnType<F, T> = { ... }               // ... this type cannot be implemented

type B = ConditionalReturnType<typeof method, number>    // type B: ???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants