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]
  }>
相关推荐
丰锋ff34 分钟前
基于 Qt 的智能门禁系统(二)
开发语言·qt
rockey62744 分钟前
C#脚本引擎之AScript与Jurassic、Jint对比
javascript·c#·.net·js·script·动态脚本
fpcc1 小时前
跟我学C++中级篇——编译期的条件选择
开发语言·c++
SomeB1oody1 小时前
【RustyML入门】6.3. 并行归约
开发语言·后端·机器学习·rust·教程
Profile排查笔记2 小时前
指纹浏览器哪个好?从 Profile、代理、权限和自动化能力判断是否适合
前端·人工智能·后端·自动化
mqiqe2 小时前
AgentScope Java 2.0 协议集成全景解析:A2A、AG-UI、Agent Protocol 三大开放协议实战指南
java·开发语言·ui
lzhdim2 小时前
12、JavaScript常见的内存泄露问题 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
denggun123452 小时前
yield
前端·数据库·python
前端snow2 小时前
ai agent -- prompt汇总
前端
脉动数据行情2 小时前
Java 实现台股 TWSE/TPEx 行情采集(个股 + K 线)
java·开发语言·twse·tpex·台股