我正在为我的项目写一个声明。
像这样:
// storage.d.ts
interface Session {
get: (key: string) => string;
save: (key: string, value: string) => void;
remove: (key: string, value: string) => void;
clear: () => void;
}
declare namespace Storage {
function get(key: string): string;
function save(key: string, value: string, time?: number);
function remove(key: string);
function clear(): void;
const session: Session
function on(key: string, fn: Function): void
function off(key: string, fn?: Function): void;
}
export as namespace Storage;
export = Storage;
我将项目发布到NPM。
现在我已经在一个项目中介绍了这个软件包。
import Storage from 'xxx'
Storage.get('xxx')
奇怪的是,VSCode不会警告任何错误。但是控制台会不断提示错误。
error in E:\my_ptoject\node_manage\admin-permission-demo\src\models\login.ts
[tsl] ERROR in E:\my_ptoject\node_manage\admin-permission-demo\src\models\login.ts(35,23)
TS2339: Property 'get' does not exist on type '{ new (): Storage; prototype: Storage; }'.
error in E:\my_ptoject\node_manage\admin-permission-demo\src\models\login.ts
[tsl] ERROR in E:\my_ptoject\node_manage\admin-permission-demo\src\models\login.ts(36,20)
TS2339: Property 'get' does not exist on type '{ new (): Storage; prototype: Storage; }'.