vue3 里的 ts 类型工具函数

目录

前言

相关 API 源码

一、PropType<T>

用于在用运行时 props 声明时给一个 prop 标注更复杂的类型定义。

示例:

typescript 复制代码
import type { PropType } from 'vue'

interface Book {
  title: string
  author: string
  year: number
}

export default {
  props: {
    book: {
      // 提供一个比 `Object` 更具体的类型
      type: Object as PropType<Book>,
      required: true
    }
  }
}

二、MaybeRef<T>

T | Ref 的别名。对于标注组合式函数的参数很有用。

三、MaybeRefOrGetter<T>

T | Ref | (() => T) 的别名。对于标注组合式函数的参数很有用。

四、ExtractPropTypes<T>

从运行时的 props 选项对象中提取 props 类型。提取到的类型是面向内部的,也就是说组件接收到的是解析后的 props。这意味着 boolean 类型的 props 和带有默认值的 props 总是一个定义的值,即使它们不是必需的。

要提取面向外部的 props,即父组件允许传递的 props,请使用 ExtractPublicPropTypes。

示例:

typescript 复制代码
const propsOptions = {
  foo: String,
  bar: Boolean,
  baz: {
    type: Number,
    required: true
  },
  qux: {
    type: Number,
    default: 1
  }
} as const

type Props = ExtractPropTypes<typeof propsOptions>
// {
//   foo?: string,
//   bar: boolean,
//   baz: number,
//   qux: number
// }

五、ExtractPublicPropTypes<T>

从运行时的 props 选项对象中提取 prop。提取的类型是面向外部的,即父组件允许传递的 props。

示例:

typescript 复制代码
const propsOptions = {
  foo: String,
  bar: Boolean,
  baz: {
    type: Number,
    required: true
  },
  qux: {
    type: Number,
    default: 1
  }
} as const

type Props = ExtractPublicPropTypes<typeof propsOptions>
// {
//   foo?: string,
//   bar?: boolean,
//   baz: number,
//   qux?: number
// }

六、ComponentCustomProperties

用于增强组件实例类型以支持自定义全局属性。

示例:

typescript 复制代码
import axios from 'axios'

declare module 'vue' {
  interface ComponentCustomProperties {
    $http: typeof axios
    $translate: (key: string) => string
  }
}

七、ComponentCustomOptions

用来扩展组件选项类型以支持自定义选项。

示例:

typescript 复制代码
import { Route } from 'vue-router'

declare module 'vue' {
  interface ComponentCustomOptions {
    beforeRouteEnter?(to: any, from: any, next: () => void): void
  }
}

八、ComponentCustomProps

用于扩展全局可用的 TSX props,以便在 TSX 元素上使用没有在组件选项上定义过的 props。

示例:

typescript 复制代码
declare module 'vue' {
  interface ComponentCustomProps {
    hello?: string
  }
}

export {}

九、CSSProperties

用于扩展在样式属性绑定上允许的值的类型。

示例:

typescript 复制代码
declare module 'vue' {
  interface CSSProperties {
    [key: `--${string}`]: string
  }
}
html 复制代码
<div style={ { '--bg-color': 'blue' } }>
html 复制代码
<div :style="{ '--bg-color': 'blue' }"></div>

【注意】类型增强必须被放置在一个模块 .ts 或 .d.ts 文件中。查看类型增强指南了解更多细节。

【拓展】*SFC <style> 标签支持通过 v-bind CSS 函数来链接 CSS 值与组件状态。这允许在没有类型扩展的情况下自定义属性。*具体请参见CSS 中的 v-bind()。

【参考文章】
vue - TypeScript 工具类型

相关推荐
北冥有鱼11 小时前
发版后动态路由点击报 `Failed to fetch dynamically imported module`」问题上的完整排查与修复过程
前端·vue.js
miofly12 小时前
GitHub 今日推荐|rea:用 MCP 把反编译器和 AI agent 接到一起的逆向工具
typescript·开源·github
apollo_qwe14 小时前
大模型 API 接通了,AI 聊天页面为什么还是不好用?
前端·vue.js·人工智能
小郎君65819 小时前
keep-alive 页面怎么“真刷新”?别改外层 key——一个 refreshVersion 的完整实现
vue.js·算法
徐小夕20 小时前
JitWord 4.3 发布!Word 内嵌 Visio 图表、原生流程图一键无损解析
前端·vue.js·github
ZoeLandia1 天前
React11:数据请求与 TanStack Query
前端·react.js·typescript·react
king王一帅1 天前
做了三年 ProseMirror 后,我用四个月自研排版引擎实现 WPS / Google Docs 替代品,整套框架 MIT 开源
前端·vue.js·开源
仰望.1 天前
vue 表格组件 vxe-table 树表格懒加载支持拖拽排序的详细用法
前端·javascript·vue.js·vxe-table
康小旺2 天前
AI 写 Vue 总写成同一个味道:给它装 Skills,装的不是知识是边界
vue.js·typescript
徐小夕2 天前
JitKnow AI知识库 4.7 发布|多文档推理+拖拽 Agent + 技能库,打造更懂业务的 AI 知识中台
前端·vue.js·github