首页 > 其他分享 >[Typescript] Defining exclusive properties with TypeScript

[Typescript] Defining exclusive properties with TypeScript

时间:2023-02-03 18:56:03浏览次数:65  
标签:exclusive TypeScript string Exclusive url Typescript type

type A = {other: 'string', url: 'string'}
type B = {other: 'string', ids: 'string'}

type Exclusive<
  T extends Record<PropertyKey, unknown>,
  U extends Record<PropertyKey, unknown>
> =
  | (T & { [k in Exclude<keyof U, keyof T>]?: never })
  | (U & { [k in Exclude<keyof T, keyof U>]?: never })

type x = Exclusive<A, B>
/*
(A & {
    ids?: undefined;
}) | (B & {
    url?: undefined;
})
*/

 

https://miyauchi.dev/posts/exclusive-property/

标签:exclusive,TypeScript,string,Exclusive,url,Typescript,type
From: https://www.cnblogs.com/Answer1215/p/17090223.html

相关文章