首页 > 其他分享 >[Typescript] DistributiveOmit

[Typescript] DistributiveOmit

时间:2023-09-02 19:22:07浏览次数:48  
标签:DistributiveOmit Typescript string Union Omit user type

Omit on Union type

type Union =
  | {
      a: "a";
      user?: string;
    }
  | {
      b: "b";
      user?: string;
    };

type X = Omit<Union, "user">; // X is {}

 

Using DistributiveOmit:

type DistributiveOmit<T, TOmitted extends PropertyKey> = T extends any
  ? Omit<T, TOmitted>
  : never;
type Union =
  | {
      a: "a";
      user?: string;
    }
  | {
      b: "b";
      user?: string;
    };

type X = DistributiveOmit<Union, "user">;
/*
Omit<{
    a: "a";
    user?: string | undefined;
}, "user"> | Omit<{
    b: "b";
    user?: string | undefined;
}, "user">
*/

 

标签:DistributiveOmit,Typescript,string,Union,Omit,user,type
From: https://www.cnblogs.com/Answer1215/p/17674089.html

相关文章

  • [React Typescript] Fixing forwardRef's Type
    FixforwardRefgloballyTojumpaheadtothesolution,uncommentingthefollowingcodefromStefanBaumgartnerwillgloballyoverridethevalueof forwardRef:declaremodule"react"{ functionforwardRef<T,P={}>( render:(props:P,r......
  • [React Typescript] Strongly type Shared props for multiple components (React.FC<
    import{Equal,Expect}from"../helpers/type-utils";typeInputProps=React.ComponentProps<"input">;constCOMPONENTS={text:(props)=>{return<input{...props}type="text"/>;},number:(p......
  • 【数据结构与算法】TypeScript 实现图结构
    classGrapg<T>{//用于存储所有的顶点verteces:T[]=[];//用于存储所有的边采用邻接表的形式adjList:Map<T,T[]>=newMap();//添加顶点addVertex(v:T){this.verteces.push(v);//初始化顶点的邻接表this.adjList.set(v,[]);}......
  • [React Typescript] Strongly type Render prop
    1.React.ReactNodeimport{useState}from"react";import{createPortal}from"react-dom";import{Equal,Expect}from"../helpers/type-utils";interfaceModalChildProps{isOpen:boolean;openModal:()=>void;......
  • [React Typescript] Strongly Typing Lazy Loaded Components with Generics
    Navigatingtothetypedefinitionfor lazy by CMD+click inlocalVSCode,orinthe DefinitelyTyped repo.Wecanseethefollowingdefinition:functionlazy<TextendsComponentType<any>>( factory:()=>Promise<{default:T}>):L......
  • TypeScript – Decorator Metadata
    前言在 TypeScript–Decorator装饰器 里,我有提到TypeScript只实现了decorate的特性,把metadata的特性独立了出来。本来我以为还需要等待很长的时间他们才会实现,没想到v5.2既然推出了。哎哟,不错哦!声明:Decorator不是TypeScript语法,它是ECMAScript(AKAJavaScr......
  • TypeScript – Using Disposable
    前言TypeScriptv5.2多了一个新功能叫 Disposable。Dispose的作用是让"对象"离开"作用域"后做出一些"释放资源"的操作。很多地方都可以看到 Dispose概念。比如WebComponent的 disconnectedCallback,Angular组件的 ngOnDestroy。而对象释放资源在其它面向对象......
  • TypeScript – 冷知识
    当genericreturn遇上parameter 报错了。原因是querySelector默认返回类型是抽象的Element。而method参数要求的是具体的InputElement解决方法是传入具体的InputElement类型constinput=document.querySelector<HTMLInputElement>('.input')!;但这不是重点......
  • TypeScript(TS)JavaScript(JS)中的所有循环方法
    for循环:for(leti=0;i<array.length;i++){//循环体}for…of循环:for(constelementofarray){//循环体}forEach方法:array.forEach((element)=>{//循环体});map方法:constnewArray=array.map((element)=>{//对......
  • typescript 数组根据指定字段去重
    this.listDataIn=data.Result.data;constuniqueItems:Item[]=Array.from(newSet(this.listDataIn.map(item=>item.MyLandID))).map(id=>{returnobj.listDataIn.find(item=>item.MyLandID===id);});......