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

Type inference inconsistent when generic extends literal #15983

Closed
wkornewald opened this issue May 21, 2017 · 3 comments
Closed

Type inference inconsistent when generic extends literal #15983

wkornewald opened this issue May 21, 2017 · 3 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@wkornewald
Copy link

TypeScript Version: 2.3.2

Code

function ff<T extends {[k: string]: number | boolean}, K extends keyof T>(x: T, k: K, v: T[K]): void {
}
let val = {a: 5}
ff(val, 'a', 9)
// PROBLEM: argument of type '9' is not assignable to parameter of type '5'
ff({a: 5}, 'a', 9)

function gg<T extends {[k: string]: number | string}, K extends keyof T>(x: T, k: K, v: T[K]): void {
}
gg({a: 5}, 'a', 9)
gg(val, 'a', 9)

Expected behavior:
The type inferencer makes no difference between any of those four calls.

Actual behavior:
The type inferencer seems to treat the generic T differently based on whether there's a literal in the union of allowed types and based on whether the argument is passed from a variable or directly. The first ff() call infers a as the literal 5 while the second one infers it as number.

This happens when you have a literal in the union. I suppose boolean is equivalent to true | false? You can trigger the same behavior when replacing boolean with some string literal. However, if you use number | string like in gg the type of a becomes number in both cases.

What's unexpected here is that I've told the compiler about my allowed types: number | boolean. No other type (not even a more specific one) should be inferred unless I explicitly list it (e.g.: number | 5). Especially, some unrelated literal (boolean) shouldn't change the inference behavior of number.

@mhegazy
Copy link
Contributor

mhegazy commented May 22, 2017

boolean behaves more or less the same as true | false. Type inference uses the existence of a literal to know if it should infer literal types or widen them to their base type.

@unional
Copy link
Contributor

unional commented May 30, 2017

Similar case but likely different cause:

interface ParsedArgs {
    x: string[];       
    y?: string;
}

declare function deepEqual<U>(value: U, expected: U, message?: string): void;

let x: ParsedArgs = {} as any

deepEqual(x, { x: [] })

const expected = { x: [] }
deepEqual(x, expected) // error

http://www.typescriptlang.org/play/index.html#src=interface%20ParsedArgs%20%7B%0D%0A%20%20%20%20x%3A%20string%5B%5D%3B%20%20%20%20%20%20%20%0D%0A%20%20%20%20y%3F%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Adeclare%20function%20deepEqual%3CU%3E(value%3A%20U%2C%20expected%3A%20U%2C%20message%3F%3A%20string)%3A%20void%3B%0D%0A%0D%0Alet%20x%3A%20ParsedArgs%20%3D%20%7B%7D%20as%20any%0D%0A%0D%0AdeepEqual(x%2C%20%7B%20x%3A%20%5B%5D%20%7D)%0D%0A%0D%0Aconst%20expected%20%3D%20%7B%20x%3A%20%5B%5D%20%7D%0D%0AdeepEqual(x%2C%20expected)%0D%0A

Let me know if you want to new issue to track this one 🌷

unional added a commit to clibuilder/clibuilder that referenced this issue May 30, 2017
unional added a commit to clibuilder/clibuilder that referenced this issue May 30, 2017
* Wha'ts wrong???

* clean up tests

* Improve readme example

* Test ci

* Try random stuff

* Add diagnostics

* Fix build
microsoft/TypeScript#15983

* Remvoe ci-check
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jul 21, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jul 21, 2018

Seems fixed in latest.

@mhegazy mhegazy closed this as completed Jul 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants