obsidian-typings
v2.7.0
Published
Extended type definitions for the Obsidian API (https://obsidian.md)
Downloads
1,147
Readme
Obsidian Extended Typings
This repository contains TypeScript typings for undocumented Obsidian API methods and variables, including additional descriptions and example uses.
Be aware that the typings currently only cover a subset of the full API: while most of the App
interface and its sub-interfaces are covered, this package does not yet provide typings for views like Graph
, Canvas
, ... — contributions for these would be very welcome!
Set-up
Installation
Install using:
npm install --save-dev obsidian-typings
Add
obsidian-typings
totypes
intsconfig.json
(recommended)If you want to have all overridden types of
obsidian-typings
available in your project without explicit imports, add the following to yourtsconfig.json
:{ "compilerOptions": { "...": "...", "types": [ "obsidian-typings" ] } }
[!WARNING]
If you added the
types
field to yourtsconfig.json
, and@types/some-package-name
does not get recognized anymore, you may need to re-add it totypes
:{ "compilerOptions": { "...": "...", "types": [ "obsidian-typings", "some-package-name" ] } }
Explicit type importing
If you prefer not to add
obsidian-typings
to yourtypes
, you can also addimport 'obsidian-typings';
to any project file.Using
obsidian-typings/implementations
Depending on how your project is set up,
import { X } from 'obsidian-typings/implementations';
may not work straight out of the box, e.g., if you have"moduleResolution": "node"
or"node10"
in yourtsconfig.json
To solve this, you can add the following to your
tsconfig.json
:{ "compilerOptions": { "...": "...", "paths": { "obsidian-typings/implementations": [ "./node_modules/obsidian-typings/dist/implementations.d.ts", "./node_modules/obsidian-typings/dist/implementations.cjs" ] } } }
Usage
obsidian
module internals
To access types from the obsidian
module, the import syntax does not change:
import { App } from 'obsidian';
function printInternalPlugins(app: App): void {
console.log(app.internalPlugins);
}
obsidian-typings
additional interfaces
Additional interfaces added by this package (which do not exist in the official API), can be imported using:
import { InternalPlugins } from 'obsidian-typings';
const internalPlugins: InternalPlugins = this.app.internalPlugins;
obsidian-typings/implementations
Additional helper functions/types/... added by this package can be used by importing from obsidian-typings/implementations
:
import { InternalPluginName } from 'obsidian-typings/implementations';
this.app.internalPlugins.getEnabledPluginById(InternalPluginName.FileExplorer);
(The list of all available implementations can be found in the implementations folder.)
Extend with your own typings
If you need to extend the typings provided by this package, add the following to any .d.ts
file in your project:
export {}; // This is a very essential line. If you don't have any other top-level `import/export` statements, those typings will work not as expected.
declare module 'obsidian-typings' {
interface PluginsPluginsRecord {
myPlugin: MyPlugin;
}
}
Disclaimer
[!WARNING]
Make sure to read below section in detail before using these typings.
Please be aware that there is a good reason why (some of) the functions and types defined here are not included with the official API definitions:
- The methods are not fully defined, and will be changed or removed in the near-future
- There is a high risk of the code behaving unexpectedly if used improperly
- The function was never meant to be used
Please use the functions and variables provided with caution. Be prepared to update your code if the API changes, and only use the functions if you are confident that you understand what they will do. Reference the official API first to see if your problem may be solved with a documented function, or search in the #plugin-dev channel of the Obsidian Discord server. Some functions will also contain @remark
TSDoc tags that provide alternatives or better solutions.
Methods marked @internal
are especially risky to use: these are either not fully typed yet, or are solely intended to be used internally by the Obsidian app.
Furthermore, there is a very high chance that there are mistakes in the typings, despite best efforts. All types had to be deduced from either context, manually running the function, or from the minified app code. You should verify that the code behaves as expected, both with regard to the expected (input/output)types, as well as what the function description promises.
With these scary disclaimers out of the way, hopefully these typings will help you in removing 90% of the @ts-ignore
s you have in your codebase, or discover solutions that didn't seem possible before.
[!NOTE]
TL;DR: Use at your own risk, verify that the code behaves as expected, and be prepared to update your code if the API changes.
@internal
methods are especially risky to use.
@remark
tags give some warnings about the inputs/outputs of the function, or provide better alternatives.
@tutorial
gives additional information on how to use the function in your plugin.
Migration
If you were using a 1.x.x
version of this package, you may need to follow the Migration guide after updating to 2.0.0
or newer.
Contributing
Feel free to start typing any part of the Obsidian API that is not yet typed, or fixing/adding additional descriptions to existing typings. If you are unsure about anything, don't hesitate to open an issue.
A brief tutorial is available on how you can get started with adding new typings, or fixing existing ones, see: CONTRIBUTING.md.