microsoft / microsoft/TypeScript

Instantiable types from return types of contextual signatures don't provide contextual type information for return expressions

Open
#61,197 0 comments 0 reactions 0 assignees View on GitHub
Domain: check: Contextual Types Help Wanted Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔎 Search Terms

instantiable contextual signature return types

### 🕗 Version & Regression Information

- This is the behavior in every version I tried

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20250216#code/C4TwDgpgBAwg9gO2BAHsAysAhsgzlAXgCgooAfKAbxNNt22AFdcAuKAIgBs4sATASwQBzdgG4atKLxxY2CRp07jJAXxoVqk0vRzM27XIwDGRiLlxiJtadjb0AToKHLaK8UV4QjnLPegAzRgQjYH5EKCM-HAhMOD8AHgAVeCRUYAA+AAoaI0RkNDZkvLSAGhzEf34hNk1aRDYAJS843niHJxKoTJCUQpT84ABKQnSoItS0dJcVMsG2ADc4fl53XIR6KHo46AIIqORYv2zSWu0GPQ5uPid2MskbWSh5RTuoFSgsfH60zGjcV9OUHqVCspH8EGARgAFmxMsMCKNMoDJDomKwOIYTGYLK8tFIZPooRBFHBbqC3oNcW9XjMiINxEA

### 💻 Code

```ts
type ContextStates =
| {
status: "loading";
data: null;
}
| {
status: "success";
data: string;
};

declare function createStore(
context: TContext,
config: {
on: Record TContext>;
},
): void;

const store = createStore(
{
status: "loading",
data: null,
} as ContextStates,
{
on: {
fetch: () => ({
status: "success",
data: "hello",
}),
},
},
);
```

### 🙁 Actual behavior

`status: "success",` errors

### 🙂 Expected behavior

no error

### Additional information about the issue

Making `(ctx: TContext) => TContext` into a type parameter makes it work (see [TS playground](https://www.typescriptlang.org/play/?ts=5.8.0-dev.20250216#code/C4TwDgpgBAwg9gO2BAHsAysAhsgzlAXgCgooAfKAbxNNt22AFdcAuKAIgBs4sATASwQBzdgG4atKLxxY2CRp07jJAXxoVqk0vRzM27XIwDGRiLlxiJtadjb0AToKHLaK8UV4QjnLPegAzRgQjYH5EKCM-HAhMOD8AHhoAFXgkVGAAGmSk+ywEXH5Q8PSIBF58AAoQlDYUxGQ0AEpCAD4oOrS0LJaKmiN69NrUhsy+xH9+ITZNWkQ2ACUvON54hycM9py8gqKEFpcVLMa2ADc4fl53fvzgKHo46AIIqORYv17SGe0GPQ5uPic7Cykhssig8kUwKgKigWHww3SmGiuChXygcyoVlI-ggwCMAAs2BVmgQ2hU0ZIdExWBxDCYzBYoVopDJ9PiIIo4ECsdDGkzoVDDkRGuIgA)) but that's not natural to write and isn't broadly applicable as a workaround

Contributor guide

Open the contributing guide

Research direction

Start by running the linked TypeScript Playground reproduction and compare it with the provided generic-type workaround. Investigate why the return expression in the contextual signature is rejected, and consider the issue complete when the original example type-checks without the workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.