Vue TypeScript Prop Type Definitions

1.9.0 · active · verified Sun Apr 19

vue-ts-types is a lightweight, TypeScript-first library designed to simplify and enhance the definition of Vue.js component props. It provides a fluent API for declaring prop types, addressing common pain points such as verbose prop declarations (especially when using Vue's built-in PropType utility), error-prone optional complex prop annotations (e.g., forgetting to union with `| undefined`), contradictions between `required` and `default` properties which lead to ambiguous behavior and ESLint warnings, and the inability to provide helpful custom validation error messages beyond a boolean result. The library is currently at version 1.9.0 and maintains an active release cadence with frequent minor updates. It supports both Vue 2.6+ and Vue 3.2+, shipping with full TypeScript type definitions. Since v1.9.0, it provides dual CommonJS and ES Module support, ensuring broad compatibility. Its core value lies in making prop definitions significantly more concise, type-safe, and less prone to common development errors in Vue projects utilizing TypeScript.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to define various component props using `vue-ts-types`, including optional, nullable, required, and validated props, showcasing type inference, default values, and how a component using these props might be defined and instantiated.

import { defineComponent, createApp } from 'vue';
import {
  arrayProp,
  booleanProp,
  functionProp,
  isPositive,
  numberProp,
  oneOfProp,
  stringProp
} from 'vue-ts-types';

const MyComponent = defineComponent({
  props: {
    disabled: booleanProp().withDefault(false),
    // resulting prop type: boolean

    title: stringProp().optional,
    // resulting prop type: string | undefined

    description: stringProp().nullable,
    // resulting prop type: string | null

    items: arrayProp<string>().required,
    // resulting prop type: string[]

    callback: functionProp<() => void>().optional,
    // resulting prop type: (() => void) | undefined

    color: oneOfProp(['red', 'green', 'blue'] as const).withDefault('red'),
    // resulting prop type: 'red' | 'green' | 'blue'

    timeout: numberProp(isPositive).required,
    // resulting prop type: number
  },
  template: `
    <div>
      <h1 v-if="title">{{ title }}</h1>
      <p v-if="description">{{ description }}</p>
      <button :disabled="disabled" @click="callback">Click Me</button>
      <ul>
        <li v-for="item in items" :key="item">{{ item }}</li>
      </ul>
      <p>Selected color: {{ color }}</p>
      <p>Timeout (ms): {{ timeout }}</p>
    </div>
  `,
  setup(props) {
    // console.log(props.timeout);
    return { };
  }
});

// To run this in a browser, you would typically mount it:
// const app = createApp(MyComponent);
// app.mount('#app');

// Example usage if you were creating a root app:
const app = createApp({
  components: { MyComponent },
  template: `<MyComponent title="Hello Vue Props" :items="['Apple', 'Banana']" :timeout="100" />`
});

// In a real application, you'd mount this to an element in your HTML
// app.mount('#app'); // Assuming you have <div id="app"></div> in your HTML

view raw JSON →