Skip to content
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

refactor(websockets): improve type safety for WebSocketGateway options #14305

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 11 additions & 9 deletions packages/websockets/decorators/socket-gateway.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import { GatewayMetadata } from '../interfaces';
* @publicApi
*/
export function WebSocketGateway(port?: number): ClassDecorator;
export function WebSocketGateway<
T extends Record<string, any> = GatewayMetadata,
>(options?: T): ClassDecorator;
export function WebSocketGateway<
T extends Record<string, any> = GatewayMetadata,
>(port?: number, options?: T): ClassDecorator;
export function WebSocketGateway<
T extends Record<string, any> = GatewayMetadata,
>(portOrOptions?: number | T, options?: T): ClassDecorator {
export function WebSocketGateway<T extends GatewayMetadata>(
Copy link
Member

Choose a reason for hiding this comment

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

we can't do extends GatewayMetadata here as GatewayMetadata interface is specific to socket.io. What if someone uses ws instead? (a different adapter)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image
Even for ws, it still calls this method

Copy link
Member

Choose a reason for hiding this comment

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

We should update the generic argument of this method then (as T is not always a subset of GatewayMetadata)

options?: T,
): ClassDecorator;
export function WebSocketGateway<T extends GatewayMetadata>(
port?: number,
options?: T,
): ClassDecorator;
export function WebSocketGateway<T extends GatewayMetadata>(
portOrOptions?: number | T,
options?: T,
): ClassDecorator {
const isPortInt = Number.isInteger(portOrOptions as number);
// eslint-disable-next-line prefer-const
let [port, opt] = isPortInt ? [portOrOptions, options] : [0, portOrOptions];
Expand Down
Loading