Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@uni-helper/uni-ui-types

uni-helper5.2kMIT1.0.0-alpha.6TypeScript support: included

为 uni-ui 组件提供 TypeScript 类型

uni-app, uniapp, uni-ui, uniui, types, type

readme

@uni-helper/uni-ui-types

License

npm

npm downloads

起步

配置编辑器/IDE

请参考 搭配 TypeScript 使用 Vue 将 Volar(Vue 语言服务工具)添加到你的编辑器或 IDE,配置完毕后请重启你的编辑器或 IDE 以确保服务启动。

[!TIP] 截至 2024-08-07,经实测确定,版本组合 Volar v2.0.28、TypeScript v5.5.4、Vue v3.4.38 可正常实现类型提示。

安装依赖

npm i -D @uni-helper/uni-ui-types

此外你需要确保项目已经安装 Vue 和 TypeScript。

[!TIP] 为了避免幽灵依赖(又称幻影依赖,详见 这篇文章),请检查你的包管理器。

如果你正在使用 yarn v2 或以上版本,请参考 文档 设置 nodeLinkernode_modules

如果你正在使用 pnpm,请参考 文档 设置 shamefully-hoisttrue

配置 TypeScript

更新 tsconfig.json,确保:

  • compilerOptions.types 包含 @uni-helper/uni-ui-types
  • include 包含 vue 相关文件

以下是一个 tsconfig.json 示例,你可以直接复制它并粘贴到项目内。请注意,你可能需要稍微调整以匹配你的开发需求,相关依赖需要自行安装。

[!WARNING] 截至 2024-07-28,uni-app 官方提供的 Vue v3 + Vite + TypeScript 模板版本相对落后,如果你正在使用它,请手动升级 TypeScript 和 Vue 版本,playground/package.json 可供参考。

你也可以在这里获取 社区模板 以起步。

{
  "compilerOptions": {
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "module": "ESNext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "jsx": "preserve",
    "jsxImportSource": "vue",
    "noImplicitThis": true,
    "strict": true,
    "verbatimModuleSyntax": true,
    "target": "ESNext",
    "useDefineForClassFields": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true,
    "types": [
      // uni-app + Vue 3 使用 Vite 构建,需要安装 vite
      "vite/client",
      // uni API 相关的 TypeScript 类型
      "@dcloudio/types",
      // my API 相关的 TypeScript 类型
      "@mini-types/alipay",
      // wx API 相关的 TypeScript 类型,需要手动安装依赖
      "miniprogram-api-typings",
      // 为 uni-ui 组件提供 TypeScript 类型
      "@uni-helper/uni-ui-types"
    ]
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue", "*.d.ts"]
}

再次重启你的编辑器或 IDE 以确保服务启动并正确加载配置。如果一切顺利,你应该可以看到类似的 TypeScript 类型提示。

示例

使用

Template

自动为 Template 内对应组件附加 TypeScript 类型提示。

示例

Script

推荐使用 @uni-helper/uni-ui-types 导出的 TypeScript 类型为变量标注类型。

<script setup lang="ts">
import { ref } from 'vue';
import type { UniBadgeType, UniBadgeOnClick } from '@uni-helper/uni-ui-types';

const type = ref<UniBadgeType>('default');
const onClick: UniBadgeOnClick = (event) => {
  // ...
};
</script>

<template>
  <uni-badge :type="type" @click="onClick"></uni-badge>
</template>

也可以直接使用命名空间来为变量标注类型(不推荐,详见 typescript-eslint - no-namespace)。

<script setup lang="ts">
import { ref } from 'vue';

const type = ref<UniHelper.UniBadgeType>('default');
const onClick: UniHelper.UniBadgeOnClick = (event) => {
  // ...
};
</script>

<template>
  <uni-badge :type="type" @click="onClick"></uni-badge>
</template>

FAQ

Vue 2 支持情况

如果你正在使用 Vue 2、需要组件 TypeScript 类型支持,请使用 Volar v2.0.21、@uni-helper/uni-ui-types v0.5 和 TypeScript v5.4.5,这是最后已知可用的版本组合。

👉 点击获取 Volar v2.0.21 VS Code Extension

如果你正在使用 Vue 2、无需组件 TypeScript 类型支持,请使用 Vetur@dcloudio/uni-helper-json

[!WARNING] Volar 和 Vetur 不可共存,请只启用其中一个。

类型不正确

该项目强依赖 Volar、TypeScript 和 Vue 内部类型和处理,更改内部类型和处理不会对使用者造成破坏性更新,但可能会对开发者造成破坏性更新,对因此更新三者任一依赖后,可能无法正常显示本身是 HTML 自带但被占用的元素的 TypeScript 类型(如 SVG 相关元素 view、image、text、输入框元素 input 等),我们强烈建议锁定依赖版本以避免类似问题。如果你遇到了类似的问题,请先回退并锁定版本,并在 QQ 群内反馈、微信群内反馈、提交 ISSUE 或 PR,我们将尽快处理,非常感谢你的帮助!🙏

类型与文档冲突或

类型与官方文档的冲突之处,请以官方文档为准,并在 QQ 群内反馈、微信群内反馈、提交 ISSUE 或 PR,我们将尽快处理,非常感谢你的帮助!🙏

致谢

最初在 uni-base-components-types 得到了灵感。

基于 这个 PR 完成。

赞助

如果这个包对你有所帮助,请考虑 赞助 支持。

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.0-alpha.6 (2024-11-06)

Bug Fixes

1.0.0-alpha.5 (2024-10-17)

Bug Fixes

  • uut: 补充 UniIcons 类型 (c688b7f) - by @

1.0.0-alpha.4 (2024-09-10)

⚠ BREAKING CHANGES

  • uut: remove UniSearchBarOnInputEvent, update UniSearchBarOnInput types
  • uni-app-components: require vue@^3.4.21 for defineOptions and shorthand v-bind

Features

  • uac: add Ad (8d4d045) - by @ModyQyW
  • uac: add AdInterstitial (be449b6) - by @ModyQyW
  • uac: add AdRewardedVideo (483bfcc) - by @ModyQyW
  • uni-app-components: add AdContentPage (3bae235) - by @ModyQyW
  • uni-app-components: add AdDraw (753606a) - by @ModyQyW
  • uni-app-components: add AdFullscreenVideo (a0919af) - by @ModyQyW
  • uni-app-components: add AdInteractive (2e77196) - by @ModyQyW

Bug Fixes

  • fix type errors (f32681e) - by @ModyQyW
  • loose peerDependencies (cb6c14b) - by @ModyQyW
  • uat: [Ad] better jsdoc styles (ab2f5c5) - by @ModyQyW
  • uat: [AdInterstitial] better jsdoc styles (0e76de1) - by @ModyQyW
  • uat: [AdRewardedVideo] better jsdoc styles (1056746) - by @ModyQyW
  • uni-app-components: add missing slot (4622ba0) - by @ModyQyW
  • uni-app-components: fix template (590a6f8) - by @ModyQyW
  • uni-app-components: require vue@^3.4.21 for defineOptions and shorthand v-bind (0033f92) - by @ModyQyW
  • uni-app-types: [AdContentPage] better jsdoc styles (794ee03) - by @ModyQyW
  • uni-app-types: [AdFullScreenVideo] better jsdoc styles (01222ce) - by @ModyQyW
  • uut: remove UniSearchBarOnInputEvent, update UniSearchBarOnInput types (2e32d7f) - by @ModyQyW

1.0.0-alpha.3 (2024-08-07)

Bug Fixes

  • uni-app-types: fix onInput types (57dedec) - by @ModyQyW

1.0.0-alpha.2 (2024-08-06)

⚠ BREAKING CHANGES

  • remove redundant types and reduce dependency on uni-app-types

Features

  • remove redundant types and reduce dependency on uni-app-types (2b03b3e) - by @ModyQyW

Bug Fixes

  • fix plugin support (#10) (1432fd3) - by @PaperStrike
  • uni-types: fix plugin exports (939c4f8) - by @ModyQyW

1.0.0-alpha.1 (2024-08-02)

Note: Version bump only for package @monorepo/uni-types

1.0.0-alpha.0 (2024-08-01)

Note: Version bump only for package @monorepo/uni-types