[memoizee]: automatically get normalizer argument type (#44045)

* feat(memoizee): automatically get normalizer argument type

* chore(memoizee): add minimum typescript version to header
This commit is contained in:
Patrick M 2020-04-27 17:06:15 +02:00 committed by GitHub
parent b8d1bf1f45
commit e773686ebb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 4 deletions

View File

@ -1,10 +1,12 @@
// Type definitions for memoizee 0.4
// Project: https://github.com/medikoo/memoizee
// Definitions by: Juan Picado <https://github.com/juanpicado>
// Patrick Muff <https://github.com/dislick>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Minimum TypeScript Version: 3.1
declare namespace memoizee {
interface Options {
interface Options<F extends (...args: any[]) => any> {
length?: number | false;
maxAge?: number;
max?: number;
@ -13,7 +15,7 @@ declare namespace memoizee {
dispose?(value: any): void;
async?: boolean;
primitive?: boolean;
normalizer?(args: any[]): string;
normalizer?(args: Parameters<F>): string;
resolvers?: Array<(arg: any) => any>;
}
@ -24,6 +26,6 @@ declare namespace memoizee {
}
// tslint:disable-next-line ban-types
declare function memoizee<F extends Function>(f: F, options?: memoizee.Options): F & memoizee.Memoized<F>;
declare function memoizee<F extends (...args: any[]) => any>(f: F, options?: memoizee.Options<F>): F & memoizee.Memoized<F>;
export = memoizee;

View File

@ -26,7 +26,7 @@ memoized.clear('bar', 7); // Dispose called with bar7 value
memoized.delete('foo', 0);
const mFn = memoize((hash: any) => {
// body of memoized function
}, { normalizer: (args: any[]) => {
}, { normalizer: (args) => {
// args is arguments object as accessible in memoized function
return JSON.stringify(args[0]);
} });
@ -59,3 +59,9 @@ setTimeout(() => {
setTimeout(() => {
memoized('foo', 3);
}, 1300);
memoize((foo: string, bar: number) => 42, {
normalizer: ([foo, bar]) => { // normalizer argument should be typed
return foo + bar.toFixed();
}
});