Microsoft/TypeScript
在 GitHub 查看Instantiable types from return types of contextual signatures don't provide contextual type information for return expressions
Open
#61,197 建立於 2025年2月16日
Domain: check: Contextual TypesHelp WantedPossible Improvement
倉庫指標
- Star
- (48,455 star)
- PR 合併指標
- (平均合併 6天 17小時) (30 天內合併 9 個 PR)
描述
🔎 Search Terms
instantiable contextual signature return types
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 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