Id生成器

158 阅读1分钟

createId

class 封装法

let id = 0
class Id{
   value: number;
   constructor(){
     id +=1
     this.value = id 
   }
}

函数封装法

let id =parseInt(window.localStorage.getItem('idMax') || '0' );
const createId = () => {
  id += 1;
  window.localStorage.setItem('idMax',JSON.stringify(id))
  return id;
};
/、写入 localStorage

其他

深拷贝

const clone = JSON.parse(JSON.stringfy(tags))

Omit

type RecordItem = {
  tagIds: number[],
  note: string,
  category: '-' | '+',
  amount: number,
  createAt:string //ISO 8601
}
type newRecordItem = Omit<RecordItem, 'createAt'>
//选用RecordItem 的类型,除了createAt

Partial

Partial<typeof selected>
//获取部分类型