Microsoft/TypeScript

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

Open

#61 197 ouverte le 16 févr. 2025

Voir sur GitHub
 (0 commentaires) (0 réactions) (0 assignés)TypeScript (6 726 forks)batch import
Domain: check: Contextual TypesHelp WantedPossible Improvement

Métriques du dépôt

Stars
 (48 455 stars)
Métriques de merge PR
 (Merge moyen 6j 17h) (9 PRs mergées en 30 j)

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

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

declare function createStore<TContext>(
  context: TContext,
  config: {
    on: Record<string, (ctx: TContext) => 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) but that's not natural to write and isn't broadly applicable as a workaround

Guide contributeur