Skip to content

Commit

Permalink
fix: server$ and AbortSignal types
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Dec 14, 2023
1 parent a1c68ec commit 2e36f16
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
9 changes: 9 additions & 0 deletions packages/qwik-city/runtime/src/server-functions.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ describe('types', () => {
expectTypeOf(foo).returns.not.toMatchTypeOf<(meep: boolean) => Promise<string>>();
});

test('matching with args', () => () => {
const foo = () => server$((name: string) => 'hello ' + name);

expectTypeOf(foo).not.toBeAny();
expectTypeOf(foo).returns.toMatchTypeOf<(name: string) => Promise<string>>();
expectTypeOf(foo).returns.toMatchTypeOf<(sig: AbortSignal, name: string) => Promise<string>>();
expectTypeOf(foo).returns.not.toMatchTypeOf<(meep: boolean) => Promise<string>>();
});

test('inferring', () => () => {
const callIt = () =>
server$(function () {
Expand Down
3 changes: 2 additions & 1 deletion packages/qwik-city/runtime/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,5 +796,6 @@ export type ServerFunction = {
* abort the fetch when fired.
*/
export type ServerQRL<T extends ServerFunction> = QRL<
(...args: [abort: AbortSignal, ...Parameters<T>] | Parameters<T>) => ReturnType<T>
| ((abort: AbortSignal, ...args: Parameters<T>) => ReturnType<T>)
| ((...args: Parameters<T>) => ReturnType<T>)
>;
7 changes: 2 additions & 5 deletions packages/qwik/src/core/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -609,21 +609,18 @@ export type PropsOf<COMP> = COMP extends Component<infer PROPS> ? NonNullable<PR
// @public
export type PublicProps<PROPS extends Record<any, any>> = TransformProps<PROPS> & ComponentBaseProps & ComponentChildren<PROPS>;

// Warning: (ae-forgotten-export) The symbol "BivariantQrlFn" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "QrlArgs" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "QrlReturn" needs to be exported by the entry point index.d.ts
//
// @public
export type QRL<TYPE = unknown> = {
__qwik_serializable__?: any;
__brand__QRL__: TYPE;
(...args: TYPE extends (...args: infer ARGS) => any ? ARGS : never): Promise<TYPE extends (...args: any[]) => infer RETURN ? Awaited<RETURN> : never>;
resolve(): Promise<TYPE>;
resolved: undefined | TYPE;
getCaptured(): unknown[] | null;
getSymbol(): string;
getHash(): string;
dev: QRLDev | null;
} & BivariantQrlFn<QrlArgs<TYPE>, QrlReturn<TYPE>>;
};

// @public
export const qrl: <T = any>(chunkOrFn: string | (() => Promise<any>), symbol: string, lexicalScopeCapture?: any[], stackOffset?: number) => QRL<T>;
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik/src/core/qrl/qrl-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const createQRL = <TYPE>(
const fn = invokeFn.call(this, tryGetInvokeContext());
const result = await fn(...args);
return result;
} as QRLInternal<TYPE>;
} as any as QRLInternal<TYPE>;

const setContainer = (el: Element | undefined) => {
if (!_containerEl) {
Expand Down
23 changes: 11 additions & 12 deletions packages/qwik/src/core/qrl/qrl.public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ export type QRL<TYPE = unknown> = {
__qwik_serializable__?: any;
__brand__QRL__: TYPE;

/**
* Resolve the QRL of closure and invoke it.
*
* @param args - Closure arguments.
* @returns A promise of the return value of the closure.
*/
(
...args: TYPE extends (...args: infer ARGS) => any ? ARGS : never
): Promise<TYPE extends (...args: any[]) => infer RETURN ? Awaited<RETURN> : never>;

/** Resolve the QRL and return the actual value. */
resolve(): Promise<TYPE>;
/** The resolved value, once `resolve()` returns. */
Expand All @@ -147,18 +157,7 @@ export type QRL<TYPE = unknown> = {
getSymbol(): string;
getHash(): string;
dev: QRLDev | null;
} & BivariantQrlFn<QrlArgs<TYPE>, QrlReturn<TYPE>>;

// https://stackoverflow.com/questions/52667959/what-is-the-purpose-of-bivariancehack-in-typescript-types/52668133#52668133
type BivariantQrlFn<ARGS extends any[], RETURN> = {
/**
* Resolve the QRL of closure and invoke it.
*
* @param args - Closure arguments.
* @returns A promise of the return value of the closure.
*/
bivarianceHack(...args: ARGS): Promise<RETURN>;
}['bivarianceHack'];
};

/** @public */
export type PropFnInterface<ARGS extends any[], RET> = {
Expand Down

0 comments on commit 2e36f16

Please sign in to comment.