npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-typed-props

v0.1.2

Published

Easy to use typed prop definitions for Vue.js

Downloads

12

Readme

Vue Typed Props

For a more elegant way of typing props in tsx/render function components.

This library provides a more elegant and efficient way to define props, making it easier to manage your components and improve the overall quality of your code.

As every Vue developer knows if we want to use tsx or the h render function we are stuck with declaring props and adding type declarations via the as PropType<T> workaround.

This library aims at providing a more elegant solution to the problem.

Example usage:

// Default way:
export default defineComponent({
  props: {
    id: { type: Number as PropType<number>, required: true, default: 100 },
    title: { type: String as PropType<string>, required: true },
    description: { type: String as PropType<string | undefined> },
    isLive: { type: Boolean as PropType<boolean | undefined> },
    author: { type: Object as PropType<Author | null | undefined> },
    comments: { type: Array as PropType<Array<Comment> | undefined>, default: () => [] },
    action: { type: Function as PropType<(v: string) => 0>, required: true },
    position: { type: [String, Number] as PropType<string | number>, required: true }
  }
});

// Will become:

import prop from "vue-typed-props";

export default defineComponent({
  props: {
    id: prop.required.number(100),
    title: prop.required.string(),
    description: prop.string(),
    isLive: prop.boolean(),
    author: prop.object<Author | null>(),
    comments: prop.array<Comment>([]),
    action: prop.required.func<(v: string) => 0>(),
    position: prop.required.generic<number | string>() // this does not do runtime type checking
  }
});

Available Functions

This library covers the following types:

  • numbers with the number function
  • strings with the string function
  • booleans with the boolean function
  • objects with the object function
  • arrays with the array function
  • functions with the func function

All the above functions accept a default value as a parameter.

For required props use prop.required. instead of prop.

Generics

For generic types use the generic and required.generic functions.

Note: The generic function does not do runtime type checking and essentially just casts the value to the type you specify.

import prop from "vue-typed-props";

const sample = prop.required.generic<number | string>();

console.log(sample); // { type: undefined, required: true, default: undefined }

const withDefault = prop.generic<number | string>("hello");

console.log(withDefault); // { type: undefined, default: "hello" }

const withObject = prop.generic<{ name: string; age: number } | number>({ name: "John", age: 20 });

console.log(withObject); // { type: undefined, default: ()=> ({name: "John", age: 20}) }