microsoft / microsoft/TypeScript

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

Offen
#61,197 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: check: Contextual Types Help Wanted Possible Improvement
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie damit, die verlinkte TypeScript Playground-Reproduktion auszuführen und sie mit dem bereitgestellten Workaround für generische Typen zu vergleichen. Untersuchen Sie, warum der Rückgabeausdruck in der kontextbezogenen Signatur abgelehnt wird, und betrachten Sie das Issue als abgeschlossen, wenn das ursprüngliche Beispiel ohne den Workaround die Typprüfung besteht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.