ts:交集类型 交叉类型

除联合类型的交集是取共同的部分外,其他类型的交集是取所有

c 复制代码
  type TypeA = { a: string; b: number; };  
  type TypeB = { b: number; c: boolean; };  
  type IntersectionType = TypeA & TypeB; // { a: string; b: number; c: boolean; }
  
  type test2 = ('1' | '2') & ('1' | '3')

如果编辑器没有简化交叉类型可以自己处理下

c 复制代码
interface User {
    name: string
    age: number
    address: string
  }
  
  type UserPartialName = PartialByKeys<User, 'name'>
  type PartialByKeys<T, K> = {
    [P in keyof T as P extends K ? P : never]?: T[P]
  } & {
    [P in Exclude<keyof T, K>]: T[P]
  }
	此时UserPartialName 显示
	// type UserPartialName = {
	//   name?: string | undefined;
	// } & {
	//   age: number;
	//   address: string;
	// } 

可以人为处理下

c 复制代码
type IntersectionToObj<T> = {
    [K in keyof T]: T[K]
  }
  type PartialByKeys<T , K = any> = IntersectionToObj<{
    [P in keyof T as P extends K ? P : never]?: T[P]
  } & {
    [P in Exclude<keyof T, K>]: T[P]
  }>
相关推荐
kyriewen3 分钟前
测试妹子让我写单测,我偷偷用AI一天干完一周的活
前端·chatgpt·cursor
2601_9577808412 分钟前
Claude Code 2026年最新部署指南:从环境搭建到技能扩展
前端·人工智能·ai编程·claude
qq_4228286229 分钟前
android图形学之SurfaceControl和Surface的关系 五
android·开发语言·python
zhangfeng113329 分钟前
workbuddy 专家 “前端开发师” 结合nvidia-mistral-small-4-119b-2603 项目计划-前端界面开发.md
前端·人工智能·免费
如竟没有火炬1 小时前
用队列实现栈
开发语言·数据结构·python·算法·leetcode·深度优先
折哥的程序人生 · 物流技术专研1 小时前
《Java 100 天进阶之路》第17篇:Java常用包装类与自动装箱拆箱深入
java·开发语言·后端·面试
C+++Python2 小时前
C 语言 动态内存分配:malloc /calloc/realloc /free
c语言·开发语言
水木流年追梦2 小时前
大模型入门-应用篇3-Agent智能体
开发语言·python·算法·leetcode·正则表达式
凯瑟琳.奥古斯特2 小时前
假脱机技术原理详解
开发语言·职场和发展
IT_陈寒2 小时前
为什么Java的Stream并行处理反而变慢了?
前端·人工智能·后端