Skip to content
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

Order of ReadonlySet/ReadonlyMap in union causes differing key inference #52313

Open
jakebailey opened this issue Jan 19, 2023 · 1 comment
Open
Labels
Bug A bug in TypeScript Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros
Milestone

Comments

@jakebailey
Copy link
Member

jakebailey commented Jan 19, 2023

Bug Report

πŸ”Ž Search Terms

ReadonlyMap ReadonlySet key inference

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried.

⏯ Playground Link

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.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 24, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 24, 2023
@typescript-bot typescript-bot added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Oct 25, 2023
@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 26, 2023

πŸ‘‹ Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.


Issue body code block by @jakebailey

❌ Failed: -

  • Argument of type 'Map' is not assignable to parameter of type 'ReadonlySet | ReadonlyMap'. Type 'Map' is not assignable to type 'ReadonlyMap'. Types of property 'forEach' are incompatible. Type '(callbackfn: (value: number, key: string, map: Map) => void, thisArg?: any) => void' is not assignable to type '(callbackfn: (value: any, key: number, map: ReadonlyMap) => void, thisArg?: any) => void'. Types of parameters 'callbackfn' and 'callbackfn' are incompatible. Types of parameters 'key' and 'key' are incompatible. Type 'string' is not assignable to type 'number'.

Historical Information
Version Reproduction Outputs
4.8.2, 4.9.3, 5.0.2, 5.1.3, 5.2.2

❌ Failed: -

  • Argument of type 'Map' is not assignable to parameter of type 'ReadonlySet | ReadonlyMap'. Type 'Map' is not assignable to type 'ReadonlyMap'. Types of property 'forEach' are incompatible. Type '(callbackfn: (value: number, key: string, map: Map) => void, thisArg?: any) => void' is not assignable to type '(callbackfn: (value: any, key: number, map: ReadonlyMap) => void, thisArg?: any) => void'. Types of parameters 'callbackfn' and 'callbackfn' are incompatible. Types of parameters 'key' and 'key' are incompatible. Type 'string' is not assignable to type 'number'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros
Projects
None yet
Development

No branches or pull requests

3 participants