API Docs

Hammerspoon 2 configs are plain JavaScript, but nothing stops you writing them in TypeScript instead and compiling down to the .js file Hammerspoon actually loads. The project ships a hammerspoon.d.ts file with every module, type, and method from the API reference declared as ambient globals, so hs, console, HSRect, and the rest are all typed without any import statements — the same way they're available at global scope when Hammerspoon runs your init.js. If you haven't written a Hammerspoon 2 config at all yet, start with the Getting Started guide instead; this guide assumes you already know the runtime (object lifecycle, require(), hotkeys, hs.ui) and just want type checking on top of it.

Why bother?#

  • Autocomplete for every module and type, in any TypeScript-aware editor.
  • Compile-time errors for typos (hs.windw) and wrong argument types/counts, instead of finding out when the callback fires.
  • Inline documentation — the same - Parameter:/- Returns: text from the API reference shows up as hover tooltips, without switching to a browser.

None of this changes what runs at the other end: Hammerspoon's JavaScriptCore engine has no idea TypeScript exists. You compile .ts to .js yourself, and only the compiled output is ever loaded.

Setup#

1. Install TypeScript#

In your config directory (hs.appinfo.configDir, typically ~/.config/Hammerspoon2/):

cd ~/.config/Hammerspoon2
npm init -y
npm install --save-dev typescript

Every copy of Hammerspoon 2 ships the hammerspoon.d.ts that exactly matches it, inside the app bundle:

/Applications/Hammerspoon 2.app/Contents/Resources/hammerspoon.d.ts

Rather than copying that file (and having to remember to re-copy it after every update), symlink it into your config directory, so TypeScript always sees the definitions for whichever version of Hammerspoon 2 you currently have installed:

cd ~/.config/Hammerspoon2
ln -s "/Applications/Hammerspoon 2.app/Contents/Resources/hammerspoon.d.ts" hammerspoon.d.ts

(If you keep the app somewhere other than /Applications, adjust the path to match.)

Then save tsconfig.example.json as tsconfig.json in the same directory. Its "*.ts" include pattern picks up the symlinked hammerspoon.d.ts automatically.

Everything hammerspoon.d.ts declares is also browsable as rendered documentation, in actual TypeScript syntax rather than the API reference's JS-flavored view — useful for seeing the real overload signatures and member types at a glance: the TypeScript API reference. To read the TS docs using the in-app viewer, call hs.docs.show(null, true).

The example tsconfig's important choices: module: "commonjs" (matching how Hammerspoon's own require() works), an outDir of ./compiled so compiled output stays out of the way, and strict mode on. Point its include at wherever your .ts files actually live:

{
  "include": ["*.ts", "src/**/*.ts"]
}

3. Write your config in TypeScript#

Create config.ts:

// Show an alert when Hammerspoon loads
hs.ui.alert("Hammerspoon loaded!")

// Bind a hotkey with type checking
hs.hotkey.bind(["cmd", "alt"], "r", () => {
    console.log("Reloading config...")
    hs.reload()
}, null, null)

// Work with windows with autocomplete
const win = hs.window.focusedWindow()
if (win) {
    const frame = win.frame
    console.log(`Window size: ${frame.w} x ${frame.h}`)
}

Nothing here differs from the plain-JS version in the Getting Started guide — the same object lifecycle rule applies, and hs.ui is built the same chained way. TypeScript only adds the type layer on top; it doesn't change how any of these APIs behave.

4. Compile and load it#

npx tsc

This produces compiled/config.js. In your init.js — which always stays plain JavaScript, since it's what Hammerspoon loads directly — require the compiled output:

require("./compiled/config.js")

Remember require()'s own rule from the Getting Started guide: the leading ./ is mandatory for local files, and init.js is the one file whose top level runs at true global scope — so if config.ts sets up hotkeys or timers, requiring it from init.js is what keeps them alive long enough to matter, exactly as if that code had been written directly in init.js.

Development workflow#

Watch mode#

npx tsc --watch

Recompiles config.js every time you save config.ts. It won't reload Hammerspoon itself — pair this with a reload hotkey (hs.reload()) or the menu bar's "Reload Config" so you can pick up each recompiled build.

npm scripts#

{
  "scripts": {
    "build": "tsc",
    "watch": "tsc --watch",
    "clean": "rm -rf compiled"
  }
}

Splitting a TypeScript config across multiple files#

This works exactly like the plain-JS version covered in Splitting a growing config into multiple files — require() doesn't know or care whether the file it's loading started out as .ts, only that the compiled .js exists on disk. A common structure:

config/
  src/
    window-management.ts
    hotkeys.ts
  compiled/
    window-management.js
    hotkeys.js
  init.js
// src/window-management.ts
export function centerFocused(): void {
    const win = hs.window.focusedWindow()
    if (win) win.centerOnScreen()
}
// init.js
const { centerFocused } = require("./compiled/window-management.js")
hs.hotkey.bind(["cmd", "alt"], "c", centerFocused, null, null)

Note the plain export/no-import mix here: window-management.ts uses TypeScript's own export syntax (compiled by tsc, with module: "commonjs", into the same module.exports shape require() expects), while anything reaching into hs itself needs no import at all — hammerspoon.d.ts declares those as ambient globals, available everywhere once it's on your TypeScript include/files list.

Editor setup#

VS Code#

Works automatically — just open your config directory and start editing .ts files. Recommended extensions: ESLint, Prettier.

WebStorm / IntelliJ#

TypeScript support is built in; open the folder and go.

Vim / Neovim#

Use a TypeScript language server plugin — coc.nvim with coc-tsserver, ALE with TypeScript support, or vim-lsp with typescript-language-server.

Troubleshooting#

"Cannot find name 'hs'"#

hammerspoon.d.ts needs to be visible to the compiler. If the symlink from step 2 is sitting in your config directory alongside tsconfig.json, an include glob like "*.ts" picks it up automatically. Check that the symlink isn't dangling (ls -lL hammerspoon.d.ts should show a real file; if not, the app has moved and the link needs recreating). Alternatively, skip the symlink and point files straight at the copy in the app bundle:

{
  "files": ["/Applications/Hammerspoon 2.app/Contents/Resources/hammerspoon.d.ts"],
  "include": ["*.ts"]
}

"Cannot find module" after compiling#

This is almost always the same require() path rule from the Getting Started guide, not a TypeScript problem: require("./compiled/config.js"), not require("compiled/config.js") — a bare specifier without .//../ is treated as a lookup for a named package, not a sibling file.

Compilation errors from third-party code#

If strict mode is fighting code you don't control (a vendored file, a generated file), relax it for just that corner rather than globally — either exclude the file from include, or wrap the offending lines with an // @ts-expect-error comment, rather than turning strict off in tsconfig.json.

Benefits in action#

// Plain JS: no autocomplete, no type checking
hs.windw.focusedWindow() // Typo! Only fails at runtime, inside the callback that uses it.
// TypeScript catches the typo at compile time, before you ever reload Hammerspoon:
hs.windw.focusedWindow()
//    ~~~~~ Error: Property 'windw' does not exist on type...
//          Did you mean 'window'?

hs.window.focusedWindow() // Correct — and autocompleted for you.

Event name types#

Modules whose on(), off() and once() take a fixed set of event names declare them as a named type, e.g. HSPowerEvent for hs.power or HSWindowFilterEvent for a window filter. Each module's API reference lists them under Event Types. (hs.userdefaults and hs.distributednotifications have none, as their event names are whatever preference keys or notification names you choose.) They're plain unions of strings, so you can use them in your own types, such as a table of handlers keyed by event:

const powerHandlers: Partial<Record<HSPowerEvent, () => void>> = {
    systemWillSleep: () => console.log("Going to sleep"),
    systemDidWake: () => console.log("Woke up"),
    sessionDidResignActive: () => console.log("Switched user"),
};

// Object.keys() always returns string[] in TypeScript, so name the key type once here
for (const event of Object.keys(powerHandlers) as HSPowerEvent[]) {
    hs.power.on(event, powerHandlers[event]!);
}

A misspelt key (systemWillSlep) is a compile error instead of a handler that never fires.

Learn more#

If you hit an issue with the type definitions themselves (a missing method, a wrong type), please open an issue on the Hammerspoon 2 repository — they're generated directly from the Swift source, so a gap there usually means a doc-comment gap in the module itself.