@ahmic/autoit-js
Version:
Node.js bindings for AutoItX3.dll
102 lines (101 loc) • 4.74 kB
TypeScript
import { IKoffiCType } from 'koffi';
/**
* A nominal type is a type that is defined by its name, rather than its structure. This allows us to take
* multiple C types that would otherwise map to the same TypeScript (JavaScript) type and treat them as
* distinct types.
*
* For example, `int`, `unsigned int`, `long`, etc. are all distinct types with their own meaning and own
* features in C, however in TypeScript they all map to `number`. By using nominal types, we can make
* TypeScript display an error when you try to use an `Int` type in a function that expects an
* `UnsignedInt` type for example.
*
* Note that there is no runtime safety to this! This is purely a compile-time check to help you catch errors
* early. For example, if a C function expects an `unsigned int` and you pass in a positive `int` ignoring
* the TypeScript error, it will work fine at runtime. If you however try to pass a negative `int`, you will
* get a runtime error.
*
* **You have been warned!**
*
* @template T The underlying TypeScript type.
* @template U The name of the nominal type.
*
* @example
*
* type Int = Nominal<number, 'INT'>;
* type UnsignedInt = Nominal<number, 'UINT'>;
*
* function add(a: Int, b: Int): Int {
* return a + b;
* }
*
* const one: Int = 123;
* const two: Int = 456;
* const three: UnsignedInt = 789;
*
* add(one, two); // OK
* add(one, three); // Error
*/
export type Nominal<T, U> = T & {
[Symbol.species]?: U;
__jsType?: T;
};
export type JsType<T extends Nominal<unknown, unknown>> = NonNullable<T['__jsType']>;
export type CType<T extends Nominal<unknown, unknown>> = NonNullable<T[typeof Symbol.species]>;
export type Win32Type<T extends Nominal<unknown, unknown> | null> = IKoffiCType & {
__jsType?: JsType<NonNullable<T>>;
[Symbol.species]?: CType<NonNullable<T>>;
};
export declare const AU3_INTDEFAULT = -2147483647;
export declare const SW_SHOWNORMAL = 1;
export declare const BOOL: Win32Type<Bool>;
export declare const BYTE: Win32Type<Byte>;
export declare const CHAR: Win32Type<Char>;
export declare const DWORD: Win32Type<DoubleWord>;
export declare const INT: Win32Type<Int>;
export declare const UINT: Win32Type<UnsignedInt>;
export declare const UINT_PTR: Win32Type<UnsignedIntPointer>;
export declare const WCHAR: Win32Type<WideChar>;
export declare const LONG: Win32Type<Long>;
export declare const LONG_PTR: Win32Type<LongPointer>;
export declare const VOID: Win32Type<Void>;
export declare const WORD: Win32Type<Word>;
export declare const PVOID: Win32Type<PointerToVoid>;
export declare const LPARAM: Win32Type<LongParam>;
export declare const LRESULT: Win32Type<LongResult>;
export declare const WPARAM: Win32Type<WordParam>;
export declare const LPCSTR: Win32Type<LongPointerToConstantString>;
export declare const LPWSTR: Win32Type<LongPointerToWideString>;
export declare const LPCWSTR: Win32Type<LongPointerToConstantWideString>;
export declare const LPVOID: Win32Type<LongPointerToVoid>;
export declare const HANDLE: Win32Type<Handle>;
export declare const HBITMAP: Win32Type<BitmapHandle>;
export declare const HDC: Win32Type<DeviceContextHandle>;
export declare const HINSTANCE: Win32Type<InstanceHandle>;
export declare const HMENU: Win32Type<MenuHandle>;
export declare const HWND: Win32Type<WindowHandle>;
export type Bool = Nominal<boolean, 'BOOL'>;
export type Byte = Nominal<number, 'BYTE'>;
export type Char = Nominal<string, 'CHAR'>;
export type DoubleWord = Nominal<number, 'DWORD'>;
export type Int = Nominal<number, 'INT'>;
export type UnsignedInt = Nominal<number, 'UINT'>;
export type UnsignedIntPointer = Nominal<number, 'UINT_PTR'>;
export type WideChar = Nominal<string, 'WCHAR'>;
export type Long = Nominal<number, 'LONG'>;
export type LongPointer = Nominal<number, 'LONG_PTR'>;
export type Void = Nominal<void, 'VOID'>;
export type Word = Nominal<number, 'WORD'>;
export type PointerToVoid = Nominal<IKoffiCType, 'PVOID'>;
export type LongParam = Nominal<number | bigint, 'LPARAM'>;
export type LongResult = Nominal<number, 'LRESULT'>;
export type WordParam = Nominal<number, 'WPARAM'>;
export type LongPointerToConstantString = Nominal<string, 'LPCSTR'>;
export type LongPointerToWideString = Nominal<string, 'LPWSTR'>;
export type LongPointerToConstantWideString = Nominal<string, 'LPCWSTR'>;
export type LongPointerToVoid = Nominal<IKoffiCType, 'LPVOID'>;
export type Handle = Nominal<bigint, 'HANDLE'>;
export type BitmapHandle = Nominal<JsType<Handle>, 'HBITMAP'>;
export type DeviceContextHandle = Nominal<JsType<Handle>, 'HDC'>;
export type InstanceHandle = Nominal<JsType<Handle>, 'HINSTANCE'>;
export type MenuHandle = Nominal<JsType<Handle>, 'HMENU'>;
export type WindowHandle = Nominal<JsType<Handle>, 'HWND'>;