Cross type

T & U example: type IPerson {name: string;
  age: number;
}

type IMan {
  love: string;
  age: number;
}
type mixin = IPerson & IMan
/ / {
// name: string;
// age: number;
// love: string;
/ /}
Copy the code

The joint type

T | U

interface IPerson {
  name: string;
  age: number;
}

interface IMan {
  love: string;
  age: number;
}

type me = IPerson | IMan;
/ / {
// age: number;
/ /}
Copy the code

Type assertion/type protection

assertionsasProtect is interface IPerson {name: string;
  age: number;
}
function isIPerson(obj: IPerson | IMan) :obj is IPerson {
  return (obj asIPerson).name ! = =undefined;
}
Copy the code

attribute

Read-only attribute ReadOnly Any attribute Any Optional attribute? Force parsing! interface Person { readonly id: number; name: string; age? : number; [propName: string]: any; }Copy the code

Conditions in the

Exclude<T, U> -- Exclude from T any type that can be assigned to U. Extract<T, U> -- Extract a type from T that can be assigned to U. NonNullable<T> -- remove from Tnullandundefined. ReturnType<T> -- Gets the return value type of the function. InstanceType<T> -- gets the InstanceType of the constructor type.Copy the code

Partial types

// Make all types optional

type User = {
    id: number,
    name: string
}

type PartialUser = MyPartial<User>

//PartialUser => {id? : number, name? : string }Copy the code

Pick the Type and Keys > function

Pick

const User = {
    name: "Zhang".age: 18,}const nameValue = pick(User, "name")/ / hu
Copy the code

Omit

// Find the attributes of the generic T other than K
type Props = { name: string; age: number; visible: boolean };
type omitTy = Omit<Props, 'name'>;
//omitTy => { age: number; visible: boolean}
Copy the code

Record

// Used to build an object type that maps properties of one type to another type

interface OneTy {
  name: string;
  age: number;
}

type KeyTy = "keyone" | "keytwo" 
type RcTy = Record<KeyTy, OneTy>;

/ / the result
ReTy= > {
  keyone: {
   name: string;
   age: number;
  },
  keytwo: {
   name: string; age: number; }},Copy the code

Exclude<Type, ExcludedUnion>

// Build a new type by excluding a type from the union type
type T1 = Exclude<"a" | "b" | "c"."a" | "b">  
//type T1 = "c"
Copy the code

Extract

// Type intersection is the opposite of Exclude
type T2 = Extract<"a" | "b" | "c"."a" | "b">  
//type T2 = "a" | "b"
Copy the code

ReturnType

type T3 = ReturnType<() = > string>;  
// type T3 = string
type T4 = ReturnType<(s: string) = > void>;  
// type T4 = void
type T5 = ReturnType<(<T>() = > T)>;  
// type T5 = {}
Copy the code