Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Andarist opened this issue Feb 16, 2025 · 0 comments Β· May be fixed by #61185
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@Andarist
Copy link
Contributor

πŸ”Ž 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

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases labels Mar 20, 2025
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
2 participants