From 0e4018a25bf4fb923f83c796b834ef718f6f75f9 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Fri, 5 Apr 2024 17:01:03 -0600 Subject: [PATCH] fix(ext/net): Improve ts types for network APIs (#23228) This currently fails to type-check in deno, but we know that listener is a `Listener` here and we should be able to improve the typing: ``` let listener = Deno.listen({ port: 0 }); console.log(listener.addr.port); -> error: TS2339 [ERROR]: Property 'port' does not exist on type 'Addr'. Property 'port' does not exist on type 'UnixAddr'. let listener = Deno.listen({ port: 0 }); console.log(listener.addr.port) ``` After: ``` Check file:///tmp/test.ts ``` --- ext/net/lib.deno_net.d.ts | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/ext/net/lib.deno_net.d.ts b/ext/net/lib.deno_net.d.ts index 00689f764d..597b3d3483 100644 --- a/ext/net/lib.deno_net.d.ts +++ b/ext/net/lib.deno_net.d.ts @@ -25,7 +25,7 @@ declare namespace Deno { * * @category Network */ - export interface Listener + export interface Listener extends AsyncIterable, Disposable { /** Waits for and resolves to the next connection to the `Listener`. */ accept(): Promise; @@ -33,7 +33,7 @@ declare namespace Deno { * with errors. */ close(): void; /** Return the address of the `Listener`. */ - readonly addr: Addr; + readonly addr: A; /** * Return the rid of the `Listener`. @@ -62,14 +62,27 @@ declare namespace Deno { * * @category Network */ - export type TlsListener = Listener; + export type TlsListener = Listener; + + /** Specialized listener that accepts TCP connections. + * + * @category Network + */ + export type TcpListener = Listener; + + /** Specialized listener that accepts Unix connections. + * + * @category Network + */ + export type UnixListener = Listener; /** @category Network */ - export interface Conn extends Reader, Writer, Closer, Disposable { + export interface Conn + extends Reader, Writer, Closer, Disposable { /** The local address of the connection. */ - readonly localAddr: Addr; + readonly localAddr: A; /** The remote address of the connection. */ - readonly remoteAddr: Addr; + readonly remoteAddr: A; /** * The resource ID of the connection. * @@ -105,7 +118,7 @@ declare namespace Deno { } /** @category Network */ - export interface TlsConn extends Conn { + export interface TlsConn extends Conn { /** Runs the client or server handshake protocol to completion if that has * not happened yet. Calling this method is optional; the TLS handshake * will be completed automatically as soon as data is sent or received. */ @@ -156,7 +169,7 @@ declare namespace Deno { */ export function listen( options: TcpListenOptions & { transport?: "tcp" }, - ): Listener; + ): TcpListener; /** Options which can be set when opening a Unix listener via * {@linkcode Deno.listen} or {@linkcode Deno.listenDatagram}. @@ -182,7 +195,7 @@ declare namespace Deno { // deno-lint-ignore adjacent-overload-signatures export function listen( options: UnixListenOptions & { transport: "unix" }, - ): Listener; + ): UnixListener; /** @category Network */ export interface ListenTlsOptions extends TcpListenOptions { @@ -266,7 +279,7 @@ declare namespace Deno { export function connect(options: ConnectOptions): Promise; /** @category Network */ - export interface TcpConn extends Conn { + export interface TcpConn extends Conn { /** * Enable/disable the use of Nagle's algorithm. * @@ -292,7 +305,7 @@ declare namespace Deno { } /** @category Network */ - export interface UnixConn extends Conn { + export interface UnixConn extends Conn { /** * The resource ID of the connection. * @@ -435,7 +448,7 @@ declare namespace Deno { * @category Network */ export function startTls( - conn: Conn, + conn: TcpConn, options?: StartTlsOptions, ): Promise;