Skip to content

Commit c0f9a49

Browse files
fix(types): check MemCache.provide against typed keys and type cachedProvide
provide's compute result must match a typed key's value, its callback may take the key elements, and cachedProvide returns what its function returns rather than unknown.
1 parent a4cba17 commit c0f9a49

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

‎lib/Compiler.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,21 @@ const getConcurrentCompilationError = memoize(() =>
167167
* @typedef {unknown extends MemCacheKeyValue<K> ? EXPECTED_ANY : MemCacheKeyValue<K>} MemCacheValue
168168
*/
169169

170+
/**
171+
* What `provide` stores under a key: the key's `MemCacheKey` value, else what its function returns.
172+
* @template K, V
173+
* @typedef {unknown extends MemCacheKeyValue<K> ? V : MemCacheKeyValue<K>} MemCacheProvided
174+
*/
175+
170176
/**
171177
* A module's memo cache: reads and writes under a `MemCacheKey<V>` are typed `V`, `provide` by what its function returns.
172178
* Plain keys stay untyped for plugins.
173179
* @typedef {{
174180
* get<K extends TupleElement>(key: K, ...rest: TupleElement[]): MemCacheValue<K> | undefined,
175181
* set<K extends TupleElement>(key: K, ...restAndValue: [...TupleElement[], MemCacheValue<K>]): void,
176-
* provide<V>(...keyAndCompute: [...TupleElement[], () => V]): V,
177-
* } & Omit<import("./util/WeakTupleMap")<TupleElement[], unknown>, "get" | "set" | "provide">} MemCache
182+
* provide<K extends TupleElement, V>(key: K, ...restAndCompute: [...TupleElement[], (...key: EXPECTED_ANY[]) => MemCacheProvided<K, V>]): MemCacheProvided<K, V>,
183+
* cachedProvide<This, V>(compute: (thisArg: This, ...key: EXPECTED_ANY[]) => V, thisArg: This, args: TupleElement[]): V,
184+
* } & Omit<import("./util/WeakTupleMap")<TupleElement[], unknown>, "get" | "set" | "provide" | "cachedProvide">} MemCache
178185
*/
179186
/** @typedef {{ buildInfo: BuildInfo, references: WeakReferences | undefined, lazyRequests: string | undefined, memCache: MemCache }} ModuleMemCachesItem */
180187

‎lib/graph/ModuleGraph.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,8 +1197,12 @@ class ModuleGraph {
11971197
(this.getParentModule(dependency))
11981198
);
11991199
if (memCache !== undefined) {
1200-
return memCache.provide(dependency, this._cacheStage, ...args, () =>
1201-
fn(this, dependency, ...args)
1200+
// A dependency key carries no `MemCacheKey` type, so the value is what `fn` returns.
1201+
return memCache.provide(
1202+
/** @type {Dependency} */ (dependency),
1203+
this._cacheStage,
1204+
...args,
1205+
() => fn(this, dependency, ...args)
12021206
);
12031207
}
12041208
}

‎types.d.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20009,11 +20009,27 @@ type MemCache = {
2000920009
key: K,
2001020010
...restAndValue: [...TupleElement[], MemCacheValue<K>]
2001120011
): void;
20012-
provide<V>(...keyAndCompute: [...TupleElement[], () => V]): V;
20013-
} & Omit<WeakTupleMap<TupleElement[], unknown>, "get" | "set" | "provide">;
20012+
provide<K extends TupleElement, V>(
20013+
key: K,
20014+
...restAndCompute: [
20015+
...TupleElement[],
20016+
(...key: any[]) => MemCacheProvided<K, V>
20017+
]
20018+
): MemCacheProvided<K, V>;
20019+
cachedProvide<This, V>(
20020+
compute: (thisArg: This, ...key: any[]) => V,
20021+
thisArg: This,
20022+
args: TupleElement[]
20023+
): V;
20024+
} & Omit<
20025+
WeakTupleMap<TupleElement[], unknown>,
20026+
"get" | "set" | "provide" | "cachedProvide"
20027+
>;
2001420028
type MemCacheKeyValue<K> = K extends { readonly __memCacheValue?: infer V }
2001520029
? V
2001620030
: unknown;
20031+
type MemCacheProvided<K, V> =
20032+
unknown extends MemCacheKeyValue<K> ? V : MemCacheKeyValue<K>;
2001720033
type MemCacheValue<K> =
2001820034
unknown extends MemCacheKeyValue<K> ? any : MemCacheKeyValue<K>;
2001920035

0 commit comments

Comments
 (0)