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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@cimom/vben-types

v5.6.3

Published

用于多个 `app` 公用的工具类型,继承了 `@cimom/vben-core-typings` 的所有能力。业务上有通用的类型定义可以放在这里。

Readme

@cimom/vben-types

用于多个 app 公用的工具类型,继承了 @cimom/vben-core-typings 的所有能力。业务上有通用的类型定义可以放在这里。

安装

# 进入目标应用目录,例如 apps/xxxx-app
# cd apps/xxxx-app
pnpm add @cimom/vben-types

基本使用

// 推荐加上 type 关键字仅导入类型定义,不引入实际代码
import type { UserInfo } from '@cimom/vben-types';

// 使用类型定义声明变量
const userInfo: UserInfo = {
  id: '1',
  username: 'admin',
  realName: 'Admin User',
  avatar: 'https://example.com/avatar.jpg',
  desc: 'System Administrator',
  homePath: '/dashboard',
  token: 'xxx-xxx-xxx',
};

可用类型

用户相关类型

/**
 * 用户信息类型
 */
interface UserInfo extends BasicUserInfo {
  /**
   * 用户描述
   */
  desc: string;
  /**
   * 首页地址
   */
  homePath: string;

  /**
   * accessToken
   */
  token: string;
}

全局类型声明

该包还提供了全局类型声明,如应用配置和路由元数据的扩展:

// 应用配置原始类型
export interface VbenAdminProAppConfigRaw {
  VITE_GLOB_API_URL: string;
}

// 应用配置处理后的类型
export interface ApplicationConfig {
  apiURL: string;
}

// 全局窗口对象扩展
declare global {
  interface Window {
    _VBEN_ADMIN_PRO_APP_CONF_: VbenAdminProAppConfigRaw;
  }
}

// Vue Router 的路由元数据扩展
declare module 'vue-router' {
  interface RouteMeta extends IRouteMeta {}
}

继承的核心类型

该包继承了 @cimom/vben-core-typings 中的所有类型,下面是一些常用的核心类型:

基础类型

// 基础用户信息类型
interface BasicUserInfo {
  id: string;
  username: string;
  realName: string;
  avatar: string;
}

// 选项类型
interface SelectOption {
  label: string;
  value: string | number;
  disabled?: boolean;
  [key: string]: any;
}

// 分页参数类型
interface PaginationParams {
  page: number;
  pageSize: number;
  total?: number;
}

路由相关类型

// 路由元数据类型
interface RouteMeta {
  // 标题
  title: string;
  // 图标
  icon?: string;
  // 是否隐藏菜单
  hideMenu?: boolean;
  // 是否需要权限
  requiresAuth?: boolean;
  // 权限码
  permissions?: string[];
  // 是否保持活跃
  keepAlive?: boolean;
  // 外链
  href?: string;
  // 排序
  order?: number;
  // 是否是外链
  isExt?: boolean;
  // 是否在标签页中显示
  hideTab?: boolean;
}

使用示例

在组件中使用

<template>
  <div>
    <h1>{{ userInfo.realName }}</h1>
    <p>{{ userInfo.desc }}</p>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import type { UserInfo } from '@cimom/vben-types';

// 使用类型定义响应式数据
const userInfo = ref<UserInfo>({
  id: '1',
  username: 'admin',
  realName: 'Admin User',
  avatar: 'https://example.com/avatar.jpg',
  desc: 'System Administrator',
  homePath: '/dashboard',
  token: 'xxx-xxx-xxx',
});
</script>

在 API 请求中使用

import { request } from '@/utils/request';
import type { UserInfo } from '@cimom/vben-types';

// 定义 API 返回类型
export function getUserInfo(): Promise<UserInfo> {
  return request.get('/api/user/info');
}

// 使用 API
async function fetchUserInfo() {
  const userInfo = await getUserInfo();
  console.log(userInfo.realName);
  console.log(userInfo.homePath);
}

在路由配置中使用

import { createRouter, createWebHistory } from 'vue-router';
// 不需要显式导入 RouteMeta 类型,因为已经在全局扩展了 vue-router 模块

const routes = [
  {
    path: '/dashboard',
    component: () => import('@/views/dashboard/index.vue'),
    meta: {
      title: '仪表盘',
      icon: 'dashboard',
      requiresAuth: true,
      permissions: ['dashboard:view'],
      keepAlive: true,
      order: 1,
    },
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

export default router;

扩展类型

如果需要添加新的类型定义,可以在项目中创建新的类型文件,然后导出:

// 在项目中创建 src/types/custom.ts
import type { UserInfo } from '@cimom/vben-types';

// 扩展用户信息类型
export interface ExtendedUserInfo extends UserInfo {
  department: string;
  role: string;
  permissions: string[];
}

// 定义其他自定义类型
export interface MenuItem {
  key: string;
  title: string;
  icon?: string;
  children?: MenuItem[];
  path?: string;
  disabled?: boolean;
}

类型声明最佳实践

  1. 始终使用 type 关键字导入类型

    import type { UserInfo } from '@cimom/vben-types';
  2. 使用接口而非类型别名

    // 推荐
    interface UserState {
      userInfo: UserInfo | null;
      token: string;
    }
    
    // 不推荐
    type UserState = {
      userInfo: UserInfo | null;
      token: string;
    };
  3. 为函数添加返回类型

    function getUserInfo(): Promise<UserInfo> {
      // ...
    }
  4. 使用类型断言而非强制类型转换

    // 推荐
    const userInfo = data as UserInfo;
    
    // 不推荐
    const userInfo = <UserInfo>data;
  5. 利用类型推断

    // 让 TypeScript 自动推断类型
    const user = {
      id: '1',
      username: 'admin',
      realName: 'Admin User',
      avatar: 'https://example.com/avatar.jpg',
      desc: 'System Administrator',
      homePath: '/dashboard',
      token: 'xxx-xxx-xxx',
    }; // TypeScript 会自动推断出类型