vue3+ts 源码学习
# type userType 类型别名导读
# 通过类型起个别名
export type userType = {
username?: string;
roles?: Array<string>;
}
1
2
3
4
2
3
4
常用于给联合类型起别名
state: (): userType => ({
// 用户名
username:
storageSession().getItem<DataInfo<number>>(sessionKey)?.username ?? "",
// 页面级别权限
roles: storageSession().getItem<DataInfo<number>>(sessionKey)?.roles ?? []
})
1
2
3
4
5
6
7
2
3
4
5
6
7
# 模块
import { userType } from "./types";
1
# 用于字符串变量类型
type str = 'li' | 'zi'
let name:str = 'li'
1
2
2
# declare const storageSession 类型声明文件
当使用第三方库时,我们需要引用它的声明文件,才能获得对应的代码补全、接口提示等功能
总结概念:
.d.ts能不能包含代码实现呢?声明一下add函数去调用一下这样可不可以呢?
因为.d.ts只能包含类型信息,而不能包含具体的代码实现
npm install --save @types/lodash
1
# declare class sessionStorageProxy implements ProxyStorage
让implements关键字让class实现接口 protected表示受保护的,仅表示所在的类和子类中可见
declare class sessionStorageProxy implements ProxyStorage {
protected storage: Storage; //
constructor(storageModel: any); // 继承
/**
* @description 储存对应键名的Storage对象
* @param k 键名
* @param v 键值
*/
setItem<T>(k: string, v: T): void;
/**
* @description 获取对应键名的Storage对象
* @param k 键名
* @returns 对应键名的Storage对象
*/
getItem<T>(k: string): T;
/**
* @description 删除对应键名的Storage对象
* @param k 键名
*/
removeItem(k: string): void;
/**
* @description 删除此域的所有Storage对象
*/
clear(): void;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
上次更新: 2023/03/05, 15:03:00