Microsoft/TypeScript

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

Open

#61,197 opened on Feb 16, 2025

View on GitHub
 (0 comments) (0 reactions) (0 assignees)TypeScript (6,726 forks)batch import
Domain: check: Contextual TypesHelp WantedPossible Improvement

Repository metrics

Stars
 (48,455 stars)
PR merge metrics
 (Avg merge 6d 17h) (9 merged PRs in 30d)

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

Contributor guide