Files
tiptap/docs/guide/typescript.md
Philipp Kühn 569aa15c4f Merge branch 'main' of https://github.com/ueberdosis/tiptap into docs/remove-gridsome
# Conflicts:
#	docs/experiments/collaboration-annotation.md
#	docs/experiments/global-drag-handle.md
2021-09-17 23:45:04 +02:00

2.0 KiB
Raw Blame History

tableOfContents
tableOfContents
true

Working with TypeScript

Introduction

The whole tiptap is code base is written in TypeScript. If you havent heard of it or never used it, no worries. You dont have to.

TypeScript extends JavaScript by adding types (hence the name). It adds new syntax, which doesnt exist in Vanilla JavaScript. Its actually removed before running in the browser, but this step the compilation is important to find bugs early. It checks if you passe the right types of data to functions. For a big and complex project, thats very valuable. It means well get notified of lot of bugs, before shipping code to you.

Anyway, if you dont use TypeScript in your project, thats fine. You will still be able to use tiptap and nevertheless get a nice autocomplete for the tiptap API (if your editor supports it, but most do).

If you are using TypeScript in your project and want to extend tiptap, there are two types that are good to know about.

Types

Options types

To extend or create default options for an extension, youll need to define a custom type, here is an example:

import { Extension } from '@tiptap/core'

export interface CustomExtensionOptions {
  awesomeness: number,
}

const CustomExtension = Extension.create<CustomExtensionOptions>({
  defaultOptions: {
    awesomeness: 100,
  },
})

Command type

The core package also exports a Command type, which needs to be added to all commands that you specify in your code. Here is an example:

import { Extension } from '@tiptap/core'

declare module '@tiptap/core' {
  interface Commands<ReturnType> {
    customExtension: {
      /**
       * Comments will be added to the autocomplete.
       */
      yourCommand: (someProp: any) => ReturnType,
    }
  }
}

const CustomExtension = Extension.create({
  addCommands() {
    return {
      yourCommand: someProp => ({ commands }) => {
        // …
      },
    }
  },
})

Thats basically it. Were doing all the rest automatically.