microsoft / microsoft/TypeScript

Feature Request: Instantiate Generic Functions within Conditional Types

Open
#61,133 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔍 Search Terms

Instantiation Expressions, Conditional Types

### ✅ Viability Checklist

- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion

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

```typescript
// some method

declare function method(value: T): T

// ... this is possible

type R0 = ReturnType>

// ... but this isn't

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

type R1 = Infer
```

Related Issue: https://github.com/microsoft/TypeScript/issues/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](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgZQJ4DsYEMAecC+cAZlBCHAOQACAzsOgMYA2WwUA9DKmAKYBGEHOxoZsOCgChQkWIhTYYwBgWKly1OoxZtO3foMkSGEdDXgAVOAF4Uo3AAoK6AK4g+PKBQCUcCUZNmcABqPAww0Na2mA5IlvgANHAABkg4AFxw5omoGVlwAF65BElefly8waHhUJHICkoAPOU8EESVYdAAfBLs7HD9AHoA-H5AA)

```typescript
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)

```typescript
const Vector = (T: T) => Type.Object({ x: T, y: T, z: T })

const T = Syntax({ Vector }, `Vector`) // 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](https://www.typescriptlang.org/play/?moduleResolution=99&module=199&ssl=9&ssc=91&pln=3&pc=1#code/JYWwDg9gTgLgBAbzgFQMoGMAWBTEBDAGhQE8xsjkA5AVxACNso4BfOAMyghDgHIABAM7AAdugA2eYFAD0MUtjoQAHjwBQq9BGED4ANWzoY0OAF44AHmRxsSmNmEATASgw58APgAUyAFwoAlKbuJGQAdADydABWBjCeSEp+yETESUQAXkks-urS0nChhXAidlDCeGJwRlU4cGB4UAKM6nJkcAAKDU0ONPSMpii9DEwt8nAAktoweKLYDgMAStgw1GXI8uat2BBscPqG0OadjXNDjO7BeXDC2HM1wM4AbsB4cJqOwDDAWhVV8qpAA))

```typescript
const Vector = (T: T) => Type.Object({ x: T, y: T, z: T })

// ... internal to the parser

type ParsedNumber = TNumber

type Instanced = ReturnType> // 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.

2. What shortcomings exist with current approaches?

Cannot instantiate generic signatures within a conditional type.

3. What workarounds are you using in the meantime?

I'm not sure there are workarounds.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the conditional-type and instantiation-expression behavior described in the examples, then review related issue #22617. Done means generic function signatures can be instantiated with supplied type arguments from a conditional type, including the shown Infer and Vector cases, without changing emitted JavaScript.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.