Skip to content

fix: Remove redundant InferReturn<T> #1338

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

Merged
merged 2 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions packages/typegpu/src/core/function/fnTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,13 @@ export type InferArgs<T extends unknown[]> = {
[Idx in keyof T]: Infer<T[Idx]>;
};

export type InferReturn<T> = T extends undefined
// biome-ignore lint/suspicious/noConfusingVoidType: <void is used as a return type>
? void
: Infer<T>;

export type JsImplementation<
Args extends unknown[] | Record<string, unknown> =
| unknown[]
| Record<string, unknown>,
Args extends unknown[] | [] = unknown[] | [],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't unknown[] | [] equivalent to unknown[]? There is no type error when I change the type to Args extends unknown[] = unknown[]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's different from TS's point of view in specific scenarios, e.g., when inferring a tuple type. The change here is done for consistency reasons, as removing | [] in the definition of Implementation instead does cause a type error.

Return = unknown,
> = (
...args: Args extends unknown[] ? InferArgs<Args>
: Args extends Record<string, never> ? []
: [InferIO<Args>]
) => InferReturn<Return>;
> = (...args: InferArgs<Args>) => Infer<Return>;

export type Implementation<
Args extends unknown[] | Record<string, unknown> =
| unknown[]
| Record<string, unknown>,
Args extends unknown[] | [] = unknown[] | [],
Return = unknown,
> = string | JsImplementation<Args, Return>;

Expand Down
15 changes: 5 additions & 10 deletions packages/typegpu/src/core/function/tgpuFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ import {
type TgpuSlot,
} from '../slot/slotTypes.ts';
import { createFnCore, type FnCore } from './fnCore.ts';
import type {
Implementation,
InferArgs,
InferReturn,
JsImplementation,
} from './fnTypes.ts';
import type { Implementation, InferArgs, JsImplementation } from './fnTypes.ts';
import { stripTemplate } from './templateUtils.ts';

// ----------
Expand Down Expand Up @@ -58,7 +53,7 @@ export type TgpuFnShell<
> =
& TgpuFnShellHeader<Args, Return>
& ((
implementation: (...args: InferArgs<Args>) => InferReturn<Return>,
implementation: (...args: InferArgs<Args>) => Infer<Return>,
) => TgpuFn<Args, Return>)
& ((implementation: string) => TgpuFn<Args, Return>)
& ((
Expand All @@ -73,7 +68,7 @@ export type TgpuFnShell<
& ((
implementation: (
...args: InferArgs<Args>
) => InferReturn<Return>,
) => Infer<Return>,
) => TgpuFn<Args, Return>)
& ((implementation: string) => TgpuFn<Args, Return>);
};
Expand Down Expand Up @@ -103,7 +98,7 @@ export type TgpuFn<
Return extends AnyData = AnyData,
> =
& TgpuFnBase<Args, Return>
& ((...args: InferArgs<Args>) => InferReturn<Return>);
& ((...args: InferArgs<Args>) => Infer<Return>);

export function fn<
Args extends AnyData[] | [],
Expand Down Expand Up @@ -135,7 +130,7 @@ export function fn<
}) as TgpuFnShell<Args, Return>;
}

export function isTgpuFn<Args extends AnyData[], Return extends AnyData>(
export function isTgpuFn<Args extends AnyData[] | [], Return extends AnyData>(
value: unknown | TgpuFn<Args, Return>,
): value is TgpuFn<Args, Return> {
return !!(value as TgpuFn<Args, Return>)?.[$internal] &&
Expand Down