首頁>技術>

1、type和interface

type Obj = {name:string;age:number};interface Obj {name:string;age:number}
2、as
let name = 'zzz' as string;
3、&的使用,| 的使用
let name : string|undefined = '';let value: string & number = 15;
4、extends的使用
interface ParentProps {name:string;age:number}interface ChildProps extends ParentProps{  address:string}let obj:ChildProps = {  name:'zzz',  age:10,  address:'北京'}
5、Partial : 將物件所有內建引數轉為可選引數,相當於?:
interface ParentProps {name:string;age:number}let obj:Partial<ParentProps> = {}
6、typeof的使用
const myName = {  name:'miao'};const otherName:typeof myName = {  name:'eeee'};

舉例:參考連結github上搜“typescript-cheatsheets/react”

type AppProps = {  message: string;  count: number;  disabled: boolean;  /** array of a type! */  names: string[];  /** string literals to specify exact string values, with a union type to join them together */  status: "waiting" | "success";  /** any object as long as you dont use its properties (NOT COMMON but useful as placeholder) */  obj: object;  obj2: {}; // almost the same as `object`, exactly the same as `Object`  /** an object with any number of properties (PREFERRED) */  obj3: {    id: string;    title: string;  };  /** array of objects! (common) */  objArr: {    id: string;    title: string;  }[];  /** a dict object with any number of properties of the same type */  dict1: {    [key: string]: MyTypeHere;  };  dict2: Record<string, MyTypeHere>; // equivalent to dict1  /** any function as long as you don't invoke it (not recommended) */  onSomething: Function;  /** function that doesn't take or return anything (VERY COMMON) */  onClick: () => void;  /** function with named prop (VERY COMMON) */  onChange: (id: number) => void;  /** alternative function type syntax that takes an event (VERY COMMON) */  onClick(event: React.MouseEvent<HTMLButtonElement>): void;  /** an optional prop (VERY COMMON!) */  optional?: OptionalType;};

10
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 大資料量查詢容易OOM?試試MySQL流式查詢