-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Plugins with Typescript #303
Comments
@damianon I want to know this as well, specifically for expire.js. Were you able to find a fix? Maybe create your own ts definition file for the parts you use? |
No, I ended up creating my on wrapper to expire entries |
@damianon could you please share your solution? |
Don't have the code at hand currently, but basically I wrapped the object and added an expiration timestamp. Upon reading the value I checked the timestamp and if expired returned null |
For future reader, try this. npm i @types/store src/@types/store.d.ts interface StoreJsAPI {
// expirePlugin
set(key: string, value: any, expiration: number): any;
} import * as engine from 'store/src/store-engine';
import * as expirePlugin from 'store/plugins/expire';
const myStore = engine.createStore([], [expirePlugin]);
myStore.set('foo', 'bar', new Date().getTime() + 3000); I hope this helps. |
ts中,添加了过期时间插件,set方法由于声明的类型缺少第三个参数,导致无法使用set方法 |
posting the solution to my problem for more convenience: import engine from "store/src/store-engine";
import expirePlugin from "store/plugins/expire";
declare global {
interface StoreJsAPI {
// expirePlugin
set(key: string, value: any, expiration: number): any;
}
}
const store = engine.createStore([], [expirePlugin]);
const storeKey = "form-data";
const formExpiryDuration = 1000 * 60 * 60; // 1 Hour
export function getFormData() {
return store.get(storeKey) || {};
}
export function setFormData(data: any) {
store.set(
storeKey,
Object.assign(getFormData(), data),
new Date().getTime() + formExpiryDuration,
);
}
export function clearFormData() {
store.set(storeKey, {});
} |
I'm trying to figure out how to use plugins (expire specifically) while using typescript.
The Store.set method doesn't accept a third parameter (expiration), even if I call Store.addPlugin(Expire)
The text was updated successfully, but these errors were encountered: