打字书呆子我有这个代码:
import * as appSettings from 'application-settings';
try {
// shim the 'localStorage' API with application settings module
global.localStorage = {
getItem(key: string) {
return appSettings.getString(key);
},
setItem(key: string, value: string) {
return appSettings.setString(key, value);
}
}
application.start({ moduleName: 'main-page' });
}
catch (err) {
console.log(err);
}…VScode给了我关于如何解决这个问题的error [ts] Property 'localStorage' does not exist on type 'Global'. [2339]想法?
这是一个Nativescript应用程序。这里有整个文件/app:https://github.com/burkeholland/nativescript-todo/blob/master/app/app.ts供参考
发布于 2019-01-26 02:09:04
这是TypeScript所期望的,global类型不包括localStorage,所以它只是试图让您知道它是一个无效的属性。
您可以通过将其转换为any来简单地克服该错误。
(<any>global).localStorage = {
getItem(key: string) {
return appSettings.getString(key);
},
setItem(key: string, value: string) {
return appSettings.setString(key, value);
}
}或者,您甚至可以从通常位于项目根目录下的references.d.ts扩展global类型。如果不存在,您可以创建一个。
declare namespace NodeJS {
interface Global {
localStorage: { getItem: (key: string) => any; setItem: (key: string, value: string) => any; };
}
}https://stackoverflow.com/questions/54370475
复制相似问题