TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

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

Open Andarist opened this issue 11 months ago • 0 comments

🔎 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

Andarist avatar Feb 16 '25 20:02 Andarist