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

@white-block/types

v0.0.3

Published

A tool to resolve Props & Emits type for Vue3

Downloads

80

Readme

npm publish test Test coverage

中文文档

简介

@white-block/types用于解析 Vue3.x 中 Props 和 Emits 类型。搭配 white-block 实现 Vue 中 Props 和 Emits 类型定义和解析的一体化。

支持解析成对象形式和数组形式,更方便使用。还额外提供了数据快速转换成 markdown 表格的工具函数,用于方便搭建组件库相关文档。

使用

定义并解析Props 和 Emits

import { VueTypeResolver } from '@white-block/types'

const code = `export type Props = {
  /** 基础类型 + 非必选 */
  base?: number,
  /** 基础类型 + 必选 */
  properties: boolean
  /** 自定义TS类型 */
  custom: CustomType
  /** 联合类型 */
  union?: AAA | BBB
  /** 交叉类型 */
  intersection?: AAA & BBB
  /** 复杂类型 */
  complex: (AAA | BBB)[]
}

export type Emits = {
  /** 导出类型 */
  click: [e: MouseEvent]
}

export default {
  base: 0,
  properties: false,
  custom: a1b2c3,
  union: aaa,
  intersection: function() { return { a: 'bbb' } },
  complex: () => ['aaa', 'bbb'],
}
`

const resolver = new VueTypeResolver(code)
    

获取类型定义

获取 Props 的类型定义 - 对象形式

resolver.getExports().Props
输出
  {
    base: {
      comment: '* 基础类型 ',
      type: 'Number',
      default: 0
    },
    properties: {
      comment: '* 基础类型 + 属性 ',
      type: 'Boolean',
      default: false,
      required: true
    },
    custom: {
      comment: '* 自定义TS类型 ',
      type: 'String as CustomType',
      default: 'a1b2c3',
      required: true
    },
    union: {
      comment: '* 联合类型 ',
      type: 'String as AAA|BBB',
      default: 'aaa'
    },
    intersection: {
      comment: '* 交叉类型 ',
      type: 'Object as AAA&BBB',
      default: { a: 'bbb' }
    },
    complex: {
      comment: '* 复杂类型 ',
      type: 'Array as (AAA|BBB)[]',
      default: ['aaa', 'bbb']
    }
  }

获取 Props 的类型定义 - 数组形式

resolver.getTransformExports('Props')
输出
  [
    {
      name: 'base',
      comment: '* 基础类型 ',
      required: false,
      type: 'Number',
      ts: 'number',
      default: 0
    },
    {
      name: 'properties',
      comment: '* 基础类型 + 属性 ',
      required: true,
      type: 'Boolean',
      ts: 'boolean',
      default: false
    },
    {
      name: 'custom',
      comment: '* 自定义TS类型 ',
      required: true,
      type: 'String',
      ts: 'CustomType',
      default: 'a1b2c3'
    },
    {
      name: 'union',
      comment: '* 联合类型 ',
      required: false,
      type: 'String',
      ts: 'AAA|BBB',
      default: 'aaa'
    },
    {
      name: 'intersection',
      comment: '* 交叉类型 ',
      required: false,
      type: 'Object',
      ts: 'AAA&BBB',
      default: { a: 'bbb' }
    },
    {
      name: 'complex1',
      comment: '* 复杂类型1 ',
      required: false,
      type: 'Array',
      ts: '(AAA|BBB)[]',
      default: ['aaa', 'bbb']
    },
    {
      name: 'complex2',
      comment: '* 复杂类型2 ',
      required: false,
      type: 'Array',
      ts: '(AAA|BBB)[]',
      default: {
        a: 1,
        b: '2',
        c: false
      }
    }
  ]

获取 Emits 的类型定义 - 对象形式

resolver.getExports().Emits
输出
  {
    click: {
      comment: '* 导出类型 ',
      params: { e: 'MouseEvent' }
    }
  }

获取 Emits 的类型定义 - 数组形式

resolver.getTransformTypeExports('Emits')
输出
  [
    {
      name: 'click',
      comment: '* 导出类型 ',
      params: { e: 'MouseEvent' }
    }
  ]

工具函数

VueTypeResolver

解析器类,用于解析 Vue3.x 中 Props 和 Emits 类型。

generateMarkdownTable

工具类,用于从解析的类型数据生成 markdown表格。

...

应用例子

具体使用可参考 test 文件夹

Emits定义如下

  export type Emits = {
    /** 导出类型 */
    click: [e: MouseEvent]
  }

输出

| 名称 | 参数 | 描述 | | :-------- | :-------- | :-------- | | click | (e: MouseEvent, payload: Data) | 点击时触发 |

Props定义如下

  export const Props = {
    /** 按钮类型 */
    type: {
      type: String as PropType<ButtonType>,
      default: 'default'
    },
    /** 组件尺寸 */
    size: {
      type: String as PropType<ButtonSize>,
      default: 'medium',
      required: true
    }
  }

输出如下

| 名称 | 类型 | TS类型 | 默认值 | 说明 | 必传 | | :-------- | :-------- | :-------- | :-------- | :-------- | :--------: | | type | String | ButtonType | 'default' | 按钮类型 | N | | size | String | ButtonSize | 'medium' | 组件尺寸 | Y |