From 8b5b327b18b1cc6b0519e632d0e9b709af18820e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=B1=AA?= <504595380@qq.com> Date: Sat, 3 Dec 2022 20:15:35 +0800 Subject: feat(ext/ffi): better type hints for Deno.dlopen (#16874) --- cli/tsc/dts/lib.deno.unstable.d.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'cli/tsc/dts') diff --git a/cli/tsc/dts/lib.deno.unstable.d.ts b/cli/tsc/dts/lib.deno.unstable.d.ts index ce609736b..9e91c8800 100644 --- a/cli/tsc/dts/lib.deno.unstable.d.ts +++ b/cli/tsc/dts/lib.deno.unstable.d.ts @@ -443,7 +443,7 @@ declare namespace Deno { /** The definition of the function. */ definition: Fn; - constructor(pointer: PointerValue, definition: Fn); + constructor(pointer: PointerValue, definition: Const); /** Call the foreign function. */ call: FromForeignFunction; @@ -494,7 +494,7 @@ declare namespace Deno { Definition extends UnsafeCallbackDefinition = UnsafeCallbackDefinition, > { constructor( - definition: Definition, + definition: Const, callback: UnsafeCallbackFunction< Definition["parameters"], Definition["result"] @@ -562,6 +562,17 @@ declare namespace Deno { close(): void; } + /** + * This magic code used to implement better type hints for {@linkcode Deno.dlopen} + */ + type Cast = A extends B ? A : B; + type Const = Cast< + T, + | (T extends string | number | bigint | boolean ? T : never) + | { [K in keyof T]: Const } + | [] + >; + /** **UNSTABLE**: New API, yet to be vetted. * * Opens an external dynamic library and registers symbols, making foreign @@ -611,7 +622,7 @@ declare namespace Deno { */ export function dlopen( filename: string | URL, - symbols: S, + symbols: Const, ): DynamicLibrary; /** **UNSTABLE**: New API, yet to be vetted. -- cgit v1.2.3