Diff
介绍
获取两个接口类型中的差值属性。
ts
type Foo = {a: string;b: number;}type Bar = {a: string;c: boolean}type Result1 = Diff<Foo,Bar> // { b: number, c: boolean }type Result2 = Diff<Bar,Foo> // { b: number, c: boolean }
View on GitHubts
type Foo = {a: string;b: number;}type Bar = {a: string;c: boolean}type Result1 = Diff<Foo,Bar> // { b: number, c: boolean }type Result2 = Diff<Bar,Foo> // { b: number, c: boolean }
起点
ts
/* _____________ Your Code Here _____________ */typeDiff <O ,O1 > = any/* _____________ Test Cases _____________ */typeFoo = {name : stringage : string}typeBar = {name : stringage : stringgender : number}typeCoo = {name : stringgender : number}typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Diff <Foo ,Bar >, {gender : number }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Diff <Bar ,Foo >, {gender : number }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Diff <Foo ,Coo >, {age : string,gender : number }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Diff <Coo ,Foo >, {age : string,gender : number }>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeDiff <O ,O1 > = any/* _____________ Test Cases _____________ */typeFoo = {name : stringage : string}typeBar = {name : stringage : stringgender : number}typeCoo = {name : stringgender : number}typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Diff <Foo ,Bar >, {gender : number }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Diff <Bar ,Foo >, {gender : number }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Diff <Foo ,Coo >, {age : string,gender : number }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Diff <Coo ,Foo >, {age : string,gender : number }>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
typeDiff <O ,O1 > = {[K in keyofO | keyofO1 asK extends keyofO & keyofO1 ? never :K ]:K extends keyofO ?O [K ]:K extends keyofO1 ?O1 [K ]: never;}typeDiff2 <O ,O1 > = {[K in keyof (O &O1 ) asK extends keyof (O |O1 ) ? never :K ]:K extends keyofO ?O [K ]:K extends keyofO1 ?O1 [K ]: never;}
ts
typeDiff <O ,O1 > = {[K in keyofO | keyofO1 asK extends keyofO & keyofO1 ? never :K ]:K extends keyofO ?O [K ]:K extends keyofO1 ?O1 [K ]: never;}typeDiff2 <O ,O1 > = {[K in keyof (O &O1 ) asK extends keyof (O |O1 ) ? never :K ]:K extends keyofO ?O [K ]:K extends keyofO1 ?O1 [K ]: never;}
ts
// most populartypeDiff <O ,O1 > =Omit <O &O1 , keyof (O |O1 )>
ts
// most populartypeDiff <O ,O1 > =Omit <O &O1 , keyof (O |O1 )>