除联合类型的交集是取共同的部分外,其他类型的交集是取所有
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]
}>