Add typing for is-hotkey library

This commit is contained in:
Pierre-Marc Airoldi 2018-02-12 20:31:22 -05:00
parent a7a33da61a
commit cdccb48a58
4 changed files with 116 additions and 0 deletions

58
types/is-hotkey/index.d.ts vendored Normal file
View File

@ -0,0 +1,58 @@
// Type definitions for is-hotkey 0.1
// Project: https://github.com/ianstormtaylor/is-hotkey#readme
// Definitions by: Pierre-Marc Airoldi <https://github.com/petester42>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export interface HotKeyOptions {
byKey: boolean;
}
export interface HotKey {
which?: number;
key?: string;
altKey: boolean;
ctrlKey: boolean;
metaKey: boolean;
shiftKey: boolean;
}
/**
* Is hotkey?
*/
export function isHotkey(
hotkey: string,
options?: HotKeyOptions
): (event: KeyboardEvent) => boolean;
export function isHotkey(
hotkey: string,
options?: HotKeyOptions | KeyboardEvent,
event?: KeyboardEvent
): boolean;
export function isCodeHotkey(hotkey: string): (event: KeyboardEvent) => boolean;
export function isCodeHotkey(hotkey: string, event: KeyboardEvent): boolean;
export function isKeyHotkey(hotkey: string): (event: KeyboardEvent) => boolean;
export function isKeyHotkey(hotkey: string, event: KeyboardEvent): boolean;
/**
* Parse.
*/
export function parseHotkey(hotkey: string, options?: HotKeyOptions): HotKey;
/**
* Compare.
*/
export function compareHotkey(object: HotKey, event: KeyboardEvent): boolean;
/**
* Utils.
*/
export function toKeyCode(name: string): number;
export function toKeyName(name: string): string;
/**
* Export.
*/
export default isHotkey;

View File

@ -0,0 +1,32 @@
import {
isHotkey,
isCodeHotkey,
isKeyHotkey,
toKeyName,
toKeyCode,
parseHotkey,
compareHotkey
} from "is-hotkey";
const event = new KeyboardEvent("");
isHotkey("mod+s")(event); // $ExpectType boolean
isHotkey("mod+s", { byKey: true })(event); // $ExpectType boolean
isHotkey("mod+s", event); // $ExpectType boolean
isHotkey("mod+s", { byKey: true }, event); // $ExpectType boolean
isCodeHotkey("mod+s")(event); // $ExpectType boolean
isKeyHotkey("mod+s")(event); // $ExpectType boolean
isCodeHotkey("mod+s", event); // $ExpectType boolean
isKeyHotkey("mod+s", event); // $ExpectType boolean
toKeyName("cmd"); // $ExpectType string
toKeyCode("shift"); // $ExpectType number
parseHotkey("cmd+s"); // $ExpectType HotKey
parseHotkey("cmd+s", { byKey: true }); // $ExpectType HotKey
compareHotkey(parseHotkey("cmd+s"), event); // $ExpectType boolean

View File

@ -0,0 +1,25 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true
},
"files": [
"index.d.ts",
"is-hotkey-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }