Skip to content

Commit

Permalink
fix(types): PropFunction aliases QRL
Browse files Browse the repository at this point in the history
also extra tests for server function types
  • Loading branch information
wmertens committed Jan 17, 2024
1 parent 78e7796 commit e8a9ad8
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/docs/src/routes/api/qwik/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,7 @@
}
],
"kind": "TypeAlias",
"content": "```typescript\nexport type PropFnInterface<ARGS extends any[], RET> = {\n __qwik_serializable__?: any;\n (...args: ARGS): Promise<RET>;\n};\n```",
"content": "> Warning: This API is now obsolete.\n> \n> Use `QRL<>` instead\n> \n\n\n```typescript\nexport type PropFnInterface<ARGS extends any[], RET> = {\n __qwik_serializable__?: any;\n (...args: ARGS): Promise<RET>;\n};\n```",
"editUrl": "https://github.com/BuilderIO/qwik/tree/main/packages/qwik/src/core/qrl/qrl.public.ts",
"mdFile": "qwik.propfninterface.md"
},
Expand All @@ -1760,7 +1760,7 @@
}
],
"kind": "TypeAlias",
"content": "```typescript\nexport type PropFunction<T extends Function = (...args: any) => any> = T extends (...args: infer ARGS) => infer RET ? PropFnInterface<ARGS, Awaited<RET>> : never;\n```\n**References:** [PropFnInterface](#propfninterface)",
"content": "Alias for `QRL<T>`<!-- -->. Of historic relevance only.\n\n\n```typescript\nexport type PropFunction<T> = QRL<T>;\n```\n**References:** [QRL](#qrl)",
"editUrl": "https://github.com/BuilderIO/qwik/tree/main/packages/qwik/src/core/qrl/qrl.public.ts",
"mdFile": "qwik.propfunction.md"
},
Expand Down
13 changes: 8 additions & 5 deletions packages/docs/src/routes/api/qwik/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,10 @@ export interface ProgressHTMLAttributes<T extends Element> extends Attrs<'progre
## PropFnInterface
> Warning: This API is now obsolete.
>
> Use `QRL<>` instead
```typescript
export type PropFnInterface<ARGS extends any[], RET> = {
__qwik_serializable__?: any;
Expand All @@ -1759,14 +1763,13 @@ export type PropFnInterface<ARGS extends any[], RET> = {
## PropFunction
Alias for `QRL<T>`. Of historic relevance only.
```typescript
export type PropFunction<T extends Function = (...args: any) => any> =
T extends (...args: infer ARGS) => infer RET
? PropFnInterface<ARGS, Awaited<RET>>
: never;
export type PropFunction<T> = QRL<T>;
```
**References:** [PropFnInterface](#propfninterface)
**References:** [QRL](#qrl)
[Edit this section](https://github.com/BuilderIO/qwik/tree/main/packages/qwik/src/core/qrl/qrl.public.ts)
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik-city/runtime/src/server-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export const serverQrl = <T extends ServerFunction>(qrl: QRL<T>): ServerQRL<T> =
} else {
// Running on the client, we need to call the function via HTTP
const ctxElm = _getContextElement();
const filtered = args.map((arg) => {
const filtered = args.map((arg: unknown) => {
if (arg instanceof SubmitEvent && arg.target instanceof HTMLFormElement) {
return new FormData(arg.target);
} else if (arg instanceof Event) {
Expand Down
32 changes: 32 additions & 0 deletions packages/qwik-city/runtime/src/server-functions.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,37 @@ describe('types', () => {

expectTypeOf(callIt).not.toBeAny();
expectTypeOf(callIt).returns.toMatchTypeOf<Promise<RequestEventBase>>();

const serverGetSourceSnippet = server$(async function (
publicApiKey: string,
symbolHash: string
) {
return {
fullName: 'fullName',
count: 5,
origin: 'origin',
originUrl: 'url',
source: 'source',
};
});
expectTypeOf(serverGetSourceSnippet).not.toBeAny();
expectTypeOf(serverGetSourceSnippet('hi', 'there')).toEqualTypeOf<
Promise<{
fullName: string;
count: number;
origin: string;
originUrl: string;
source: string;
}>
>();
expectTypeOf(serverGetSourceSnippet(new AbortController().signal, 'hi', 'there')).toEqualTypeOf<
Promise<{
fullName: string;
count: number;
origin: string;
originUrl: string;
source: string;
}>
>();
});
});
6 changes: 3 additions & 3 deletions packages/qwik/src/core/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -605,14 +605,14 @@ export const PrefetchServiceWorker: (opts: {
export interface ProgressHTMLAttributes<T extends Element> extends Attrs<'progress', T> {
}

// @public (undocumented)
// @public @deprecated (undocumented)
export type PropFnInterface<ARGS extends any[], RET> = {
__qwik_serializable__?: any;
(...args: ARGS): Promise<RET>;
};

// @public (undocumented)
export type PropFunction<T extends Function = (...args: any) => any> = T extends (...args: infer ARGS) => infer RET ? PropFnInterface<ARGS, Awaited<RET>> : never;
// @public
export type PropFunction<T> = QRL<T>;

// @public @deprecated (undocumented)
export type PropFunctionProps<PROPS extends Record<any, any>> = {
Expand Down
17 changes: 10 additions & 7 deletions packages/qwik/src/core/qrl/qrl.public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,23 @@ type BivariantQrlFn<ARGS extends any[], RETURN> = {
bivarianceHack(...args: ARGS): Promise<RETURN>;
}['bivarianceHack'];

/** @public */
/**
* @deprecated Use `QRL<>` instead
* @public
*/
export type PropFnInterface<ARGS extends any[], RET> = {
__qwik_serializable__?: any;
(...args: ARGS): Promise<RET>;
};

let runtimeSymbolId = 0;

/** @public */
export type PropFunction<T extends Function = (...args: any) => any> = T extends (
...args: infer ARGS
) => infer RET
? PropFnInterface<ARGS, Awaited<RET>>
: never;
/**
* Alias for `QRL<T>`. Of historic relevance only.
*
* @public
*/
export type PropFunction<T> = QRL<T>;

// <docs markdown="../readme.md#$">
// !!DO NOT EDIT THIS COMMENT DIRECTLY!!!
Expand Down
9 changes: 8 additions & 1 deletion packages/qwik/src/core/render/jsx/types/jsx-types.unit.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertType, describe, expectTypeOf, test } from 'vitest';
import { $ } from '../../../qrl/qrl.public';
import { $, type PropFunction } from '../../../qrl/qrl.public';
import type { EventHandler, QRLEventHandlerMulti } from './jsx-qwik-attributes';
import type { FunctionComponent, JSXOutput } from './jsx-node';
import type { QwikIntrinsicElements } from './jsx-qwik-elements';
Expand Down Expand Up @@ -67,6 +67,13 @@ describe('types', () => {
EventHandler<PointerEvent, SVGSVGElement> | QRLEventHandlerMulti<PointerEvent, SVGSVGElement>
>();
});
test('PropFunction', () => () => {
const CmpButton = component$<{
onClick$?: PropFunction<() => void>;
}>((props) => <button onClick$={props.onClick$} />);

<CmpButton onClick$={() => alert('CLICKED!')}>click me!</CmpButton>;
});

test('unknown string component', () => () => {
const t = (
Expand Down

0 comments on commit e8a9ad8

Please sign in to comment.