Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions types/lru-cache/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Type definitions for lru-cache 7.4
// Type definitions for lru-cache 7.5
// Project: https://github.com/isaacs/node-lru-cache
// Definitions by: Bart van der Schoor <https://github.com/Bartvds>
// BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3

declare class LRUCache<K, V> implements Iterable<[K, V]> {
constructor(options?: LRUCache.Options<K, V>);
constructor(options: LRUCache.Options<K, V>);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is options no longer optional starting at 7.5 or was it never optional and DT was wrong? I can't seem to find a change related to that on the original library (isaacs/node-lru-cache@v7.4.0...v7.5.0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was never optional and the typings are wrong :(

See #59162


/**
* Return total length of objects in cache taking into account `length` options function.
Expand Down Expand Up @@ -100,20 +100,41 @@ declare class LRUCache<K, V> implements Iterable<[K, V]> {
rforEach<T = this>(callbackFn: (this: T, value: V, key: K, cache: this) => void, thisArg?: T): void;

/**
* Return a generator yielding the keys in the cache.
* Return a generator yielding the keys in the cache,
* in order from most recently used to least recently used.
*/
keys(): Generator<K>;

/**
* Return a generator yielding [key, value] pairs.
* Return a generator yielding the keys in the cache,
* in order from least recently used to most recently used.
*/
rkeys(): Generator<K>;

/**
* Return a generator yielding the values in the cache,
* in order from most recently used to least recently used.
*/
values(): Generator<V>;

/**
* Return an array of the entries in the cache.
* Return a generator yielding the values in the cache,
* in order from least recently used to most recently used.
*/
rvalues(): Generator<V>;

/**
* Return a generator yielding `[key, value]` pairs,
* in order from most recently used to least recently used.
*/
entries(): Generator<[K, V]>;

/**
* Return a generator yielding `[key, value]` pairs,
* in order from least recently used to most recently used.
*/
rentries(): Generator<[K, V]>;

[Symbol.iterator](): Iterator<[K, V]>;

/**
Expand Down
8 changes: 6 additions & 2 deletions types/lru-cache/lru-cache-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const foo = {
foo() {},
};

const cache = new LRUCache<string, Foo>();
const cache = new LRUCache<string, Foo>({ max: 10 });
cache; // $ExpectType LRUCache<string, Foo>

// $ExpectType LRUCache<string, Foo>
Expand Down Expand Up @@ -39,7 +39,7 @@ new LRUCache<string, Foo>({
},
});
new LRUCache<string, Foo>({ max: num }); // $ExpectType LRUCache<string, Foo>
new LRUCache<string, Foo>(); // $ExpectType LRUCache<string, Foo>
new LRUCache<string, Foo>(); // $ExpectError
// $ExpectType LRUCache<string, Foo>
new LRUCache<string, Foo>({
max: num,
Expand Down Expand Up @@ -137,7 +137,11 @@ cache.rforEach(function(value, key, cache) {
}, foo);

cache.keys(); // $ExpectType Generator<string, any, unknown>
cache.rkeys(); // $ExpectType Generator<string, any, unknown>
cache.values(); // $ExpectType Generator<Foo, any, unknown>
cache.rvalues(); // $ExpectType Generator<Foo, any, unknown>
cache.entries(); // $ExpectType Generator<[string, Foo], any, unknown>
cache.rentries(); // $ExpectType Generator<[string, Foo], any, unknown>

const dump = cache.dump();
dump; // $ExpectType [string, Entry<Foo>][]
Expand Down