microsoft / microsoft/TypeScript

Type instantiation for type level generic function

Open
#62,524 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

type instantiation
type level generic function
generic return type

Related: https://github.com/microsoft/TypeScript/issues/40542, https://github.com/microsoft/TypeScript/issues/61133 and maybe https://github.com/microsoft/TypeScript/issues/52035?

Edit: Just found https://github.com/microsoft/TypeScript/issues/55435 and https://github.com/microsoft/TypeScript/issues/40179

### ✅ 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

#47607 added support for instantiation expressions, but those do not apply at the type level. I understand it'd be ambiguous in some cases (https://github.com/microsoft/TypeScript/pull/47607#issuecomment-1058192563), but in some cases there really isn't a reasonable workaround.

### 📃 Motivating Example

I actually have a very similar case to the example provided in https://github.com/microsoft/TypeScript/issues/40542

```ts
const provideBox = () => {
const box = (value: T) => ({value});
return box;
};

type BoxMaker = ReturnType
// type BoxMaker = (value: T) => { value: T; }

type Box1 = ReturnType>;
// Type 'BoxMaker' is not generic.(2315)

const box = undefined as unknown as BoxMaker;

type Box2 = ReturnType>;
//type Box2 = { value: T; }

```

Compiler Options

```json
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"target": "ES2017",
"module": "ESNext",
"moduleResolution": "node"
}
}
```

**Playground Link:** [Provided](https://www.typescriptlang.org/play/?jsx=0#code/MYewdgzgLgBADgJxANwJYBMCmAhEAPGAXhgAoBKIgPhgG8AoGRmUSWAI3yJgB4AVSksgCGAGwCumAFwxeFQtRI1h4zAF8yAbgZMEmKGIRgYHPFtVa6UAJ5xMMXHgCyQgNaYEXAEp6DYXjcxua1sQADN4JDQsB0o6AHo4mGC7B2c3D2I+AWUJaVkqWhgcqRkNGFU6SwD7fABGLK8fQ39bblTXdyzKLQSZaoBydvT+mFQIGDAQWABzTDB3VGAAOhIAJgBmWoBWMkqWaGNOYjEwLFDUefQYIXGTl0mAdyMbmqcOhAtk19WG4m99ZoBIIBMKHPBdHpxL4OH78Lg0IqiXKlcqVIA)

### 💻 Use Cases

1. What do you want to use this for?

In our case we have a more complex `provideBox` that accept arguments. We also need to rely on type inference for the generic type of the `box` function and its return value;

3. What shortcomings exist with current approaches?

I cannot extract and export a generic type for a return value of a generic function if I don't have direct reference to the function.

5. What workarounds are you using in the meantime?

The only workaround I found is to use a dummy runtime value to apply the type, and use and instantiation expression on that dummy value (see `Box2` above)

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 by reproducing the motivating example in the provided Playground link, then read the related issues and PR #47607 to understand existing instantiation-expression behavior and the noted ambiguities. Done means a type-level form can extract the generic return type without a dummy runtime value, producing the intended Box2 result while preserving existing behavior.

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.