惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

講評世界

My App Defaults 2023 读书的意义 そして、次の曲が始まるのです 从零开始的 RSSHub Docker 私有化部署指南 使用 Homebrew 安装 Typora 的 0.11.18 版本 【译文】Grid 用于布局, Flexbox 用于组件 【译文】IndexedDB 为什么这么慢?如何更好的使用呢? Hello 2022 「他山之石」零贰 「他山之石」零壹 「言論」 零壹 给 icarus 主题增加所有文章的字数统计 hexo 无法在本地实时预览 JavaScript 立即调用的函数表达式(IIFE) 解决 nvm 无法在 arm 架构下安装 V15 以下的 node 版本 的问题 m1 芯片安装 nvm 提示 command not found 如何在 JavaScript 完美的确定一个数据的类型 Cookie?小饼干! 使用 RSS 在推荐算法中获取主动权
使用 TypeScript 为 Vue 组件的 prop 标注类型
Moeyua · 2022-06-02 · via 講評世界

在选项式 API 或者不使用 <script setup> 时我们可以使用 PropType 这个工具类型来标记更复杂的 prop 类型:

import { defineComponent } from 'vue'
// 引入 Proptype
import type { PropType } from 'vue'

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

export default defineComponent({
  props: {
    book: {
      // 提供相对 `Object` 更确定的类型
      type: Object as PropType<Book>,
      required: true
    },
    // 也可以标记函数
    callback: Function as PropType<(id: number) => void>,
    itemList: {
      // 定义数组类型
      type: Array as PropType<Array<SingleItem>>,
      required: false,
    },
  },
})

如果不使用 PropType 而直接对类型进行断言:

itemList: {
      // 项目列表
      type: Array as Array<SingleItem>,
      required: false,
    },

会得到 类型 "ArrayConstructor" 到类型 "SingleItem[]" 的转换可能是错误的,因为两种类型不能充分重叠。如果这是有意的,请先将表达式转换为 "unknown"。 这样的错误。

当使用 <script setup> 时,我们直接通过泛型参数来定义 prop 的类型:

<script setup lang="ts">
const props = defineProps<{
  foo: string
  bar?: number
}>()
</script>

defineProps() 宏函数支持从它的参数中推导类型,所以我们也可以在它的参数中定义:

<script setup lang="ts">
const props = defineProps({
  foo: { type: String, required: true },
  bar: Number
})

props.foo // string
props.bar // number | undefined
</script>