Skip to content
GitHub

TypeScript

Types for Drupal, drupalSettings and once, plus a generated tsconfig.

The plugin ships types for Drupal's JavaScript API and generates a TypeScript setup for your theme.

Setup

The uebertool binary, which runs before vite in the dev and build scripts, writes .uebertool/tsconfig.json and .uebertool/vite-env.d.ts. Extend it from your theme:

tsconfig.json
{
  "extends": "./.uebertool/tsconfig.json"
}
.uebertool/vite-env.d.ts
/// <reference types="vite/client" />
/// <reference types="@ueberbit/vite-plugin-drupal/types/dom" />
/// <reference types="@ueberbit/vite-plugin-drupal/types/drupal" />
/// <reference types="@ueberbit/vite-plugin-drupal/types/once" />
/// <reference types="@ueberbit/vite-plugin-drupal/types/virtual" />

declare module '*.vue' {
  import type { DefineComponent } from 'vue'

  const component: DefineComponent<{}, {}, any>
  export default component
}

What is typed

  • Drupal: behaviors, Drupal.t(), Drupal.Message, … and drupalSettings,
  • once() from core/once,
  • Vite's import.meta.env and import.meta.hot,
  • *.vue imports,
  • auto-imports and Vue custom element tag names (.uebertool/*.d.ts).
Drupal.behaviors.accordion = {
  attach(context) {
    once('accordion', '.accordion', context).forEach((element) => {
      element.setAttribute('aria-label', Drupal.t('Accordion'))
    })
  },
}

Generated tsconfig

.uebertool/tsconfig.json
{
  "compilerOptions": {
    "target": "ESNext",
    "useDefineForClassFields": false,
    "module": "ESNext",
    "lib": [
      "ESNext",
      "DOM",
      "DOM.Iterable"
    ],
    "moduleResolution": "Bundler",
    "strict": true,
    "sourceMap": true,
    "resolveJsonModule": true,
    "checkJs": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "noEmit": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "skipLibCheck": true,
    "allowUmdGlobalAccess": true,
    "experimentalDecorators": true,
    "jsx": "react-jsx",
    "jsxImportSource": "preact",
    "paths": {
      "~": [
        ".."
      ],
      "~/*": [
        "../*"
      ],
      "@": [
        ".."
      ],
      "@/*": [
        "../*"
      ],
      "~~": [
        ".."
      ],
      "~~/*": [
        "../*"
      ],
      "@@": [
        ".."
      ],
      "@@/*": [
        "../*"
      ],
      "assets": [
        "../assets"
      ],
      "assets/*": [
        "../assets/*"
      ],
      "public": [
        "../public"
      ],
      "public/*": [
        "../public/*"
      ]
    }
  },
  "include": [
    "../components/**/*",
    "../js/**/*",
    "../templates/**/*",
    "./components.d.ts",
    "./custom-elements.d.ts",
    "./unimport.d.ts",
    "./vite-env.d.ts"
  ],
  "exclude": [
    "**/node_modules",
    "**/dist",
    "**/*.stories.jsx",
    "**/.storybook/**/*",
    "**/*.js",
    "**/*.mdx"
  ]
}