如何在打字稿中使用条件类型?
在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是文章学习者,那么本文《如何在打字稿中使用条件类型?》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

在 typescript 中使用条件属性:一个实际示例
在 typescript 中,条件属性允许我们创建灵活且类型安全的接口,可以根据某些条件进行调整。这在处理复杂的数据结构时特别有用,其中某些属性只应在特定情况下出现。在这篇博文中,我们将通过涉及奖励组的实际示例来探索如何使用条件属性。
场景
想象一下我们有一个管理不同类型奖励的系统。每个奖励可以是特定类型,例如“金融”或“运输”。
根据奖励类型,应包含或排除某些属性。例如,财务奖励应包括财务属性,而运输奖励应包括运输属性。此外,我们希望确保仅根据奖励类型和奖励条件包含某些属性。
定义类型
首先,让我们定义我们将使用的基本类型和接口:
type rewardtype = "finance" | "shipping" | "other"; // example values for rewardtype
interface itemconditionattribute {
// define the properties of itemconditionattribute here
}
interface rewardattributes {
// define the properties of rewardattributes here
}
interface shippingattributes {
// define the properties of shippingattributes here
}
interface financeattributes {
// define the properties of financeattributes here
}
interface rewardgroupbase {
groupid: number;
rewardtype: rewardtype;
rewardon: string;
itemconditionattributes: itemconditionattribute[];
}
使用条件类型
为了确保仅当rewardtype为“finance”时才包含financeattributes,并且当rewardon为“finance”时不包含rewardattributes,我们可以使用条件类型。以下是我们定义 rewardgroup 类型的方式:
type rewardgroup = rewardgroupbase & (
{ rewardtype: "finance"; rewardon: "finance"; financeattributes: financeattributes; rewardattributes?: never; shippingattributes?: never } |
{ rewardtype: "shipping"; rewardon: exclude; shippingattributes: shippingattributes; financeattributes?: never; rewardattributes: rewardattributes } |
{ rewardtype: exclude; rewardon: exclude; financeattributes?: never; shippingattributes?: never; rewardattributes: rewardattributes }
);
说明
基本接口:
rewardgroupbase 包含始终存在的通用属性,无论奖励类型如何。
条件类型:
我们使用三种类型的联合来处理条件属性。
当rewardtype为“finance”且rewardon为“finance”时,financeattributes为必填项,
并且不允许使用rewardattributes 和shippingattributes。当rewardtype为“shipping”且rewardon不是“finance”时,shippingattributes为必填项,不允许financeattributes,但包含rewardattributes。
对于任何其他不是“finance”的rewardtype 和rewardon,将包含rewardattributes,但不包含financeattributes 和shippingattributes。
用法示例
以下是您在实践中使用 rewardgroup 类型的方法:
const financeReward: RewardGroup = {
groupId: 1,
rewardType: "FINANCE",
rewardOn: "Finance",
itemConditionAttributes: [ /* properties */ ],
financeAttributes: { /* properties */ }
};
const shippingReward: RewardGroup = {
groupId: 2,
rewardType: "SHIPPING",
rewardOn: "Delivery",
itemConditionAttributes: [ /* properties */ ],
shippingAttributes: { /* properties */ },
rewardAttributes: { /* properties */ }
};
// This will cause a TypeScript error because financeAttributes is not allowed for rewardType "SHIPPING"
const invalidReward: RewardGroup = {
groupId: 3,
rewardType: "SHIPPING",
rewardOn: "Delivery",
itemConditionAttributes: [ /* properties */ ],
financeAttributes: { /* properties */ } // Error: financeAttributes
};
今天关于《如何在打字稿中使用条件类型?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!
为什么给a标签设置宽度才能展示SVG图片?
- 上一篇
- 为什么给a标签设置宽度才能展示SVG图片?
- 下一篇
- 2023年最美电脑壁纸推荐,让你的桌面焕然一新
-
- 文章 · 前端 | 3小时前 | 前端 · css · 组合选择器 CSS :is specificity :where 级联层
- CSS :is 组合选择器时的 specificity 控制
- 327浏览 收藏
-
- 文章 · 前端 | 16小时前 | javascript · DNS耗时 PerformanceResourceTiming TLS耗时
- PerformanceResourceTiming 拆分 DNS 与 TLS 耗时
- 108浏览 收藏
-
- 文章 · 前端 | 3天前 | 前端 · websocket ·
- WebSocket 重连退避与页面卸载的收尾
- 381浏览 收藏
-
- 文章 · 前端 | 3天前 |
- Fetch keepalive 处理页面卸载前的小请求
- 291浏览 收藏
-
- 文章 · 前端 | 3天前 |
- CSS @scope 限定组件样式作用域的迁移方法
- 294浏览 收藏
-
- 文章 · 前端 | 3天前 |
- View Transition API 在 SPA 状态切换中的降级方案
- 142浏览 收藏
-
- 文章 · 前端 | 3天前 | javascript · 表单 · FormData FormDataEvent formdata事件 原生表单
- FormDataEvent 统一拦截原生表单数据的实现
- 288浏览 收藏
-
- 文章 · 前端 | 3天前 | html · javascript · 表单 · requestSubmit form.submit HTML表单校验
- requestSubmit 与 form.submit 有什么区别
- 392浏览 收藏
-
- 文章 · 前端 | 4天前 |
- scheduler.yield 怎么把长任务拆开又保持优先级
- 110浏览 收藏
-
- 文章 · 前端 | 4天前 | CSS 滚动动画 animation-timeline scroll-timeline
- CSS scroll-timeline 怎么驱动滚动进度动画
- 251浏览 收藏
-
- 文章 · 前端 | 4天前 |
- IntersectionObserver scrollMargin 怎么处理嵌套滚动容器
- 380浏览 收藏
-
- 文章 · 前端 | 4天前 |
- dialog 的 closedby 属性怎么控制关闭方式
- 269浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 293次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 346次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 352次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 315次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 135次使用
-
- JavaScript函数定义及示例详解
- 2025-05-11 502浏览
-
- 智能体安全引领产业升级——国内AI安全产品市场深度分析
- 2026-08-21 501浏览
-
- CSS变量简化按钮悬停效果技巧
- 2026-05-31 501浏览
-
- JavaScript符号类型详解与应用
- 2026-05-31 501浏览
-
- HTML剪贴板复制粘贴怎么用
- 2026-05-26 501浏览

