Open
Description
Bug Report
π Search Terms
ReadonlyMap ReadonlySet key inference
π Version & Regression Information
- This is the behavior in every version I tried.
β― Playground Link
π» Code
declare const map: Map<string, number>;
declare function forEachKey<K, T>(map: ReadonlySet<K> | ReadonlyMap<K, any>, callback: (key: K) => T | undefined): T | undefined;
forEachKey(map, (key) => {}) // Error passing "map", key type is wrong (number??)
forEachKey<string, void>(map, (key) => {}) // OK; no error, key type is string
// Now, flip the union order in the parameter's type.
declare function forEachKey2<K, T>(map: ReadonlyMap<K, any> | ReadonlySet<K>, callback: (key: K) => T | undefined): T | undefined;
forEachKey2(map, (key) => {}) // Okay?
forEachKey2<string, void>(map, (key) => {}) // Still okay.
π Actual behavior
The order of the union appears to matter, breaking inference and erroring on valid code. Swapping the union order allows the code to compile.
π Expected behavior
All of this should compile; the union ordering should not matter.