-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Comments
|
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 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
Seems fixed in latest. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeScript Version: 2.3.2
Code
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 infersa
as the literal 5 while the second one infers it asnumber
.This happens when you have a literal in the union. I suppose
boolean
is equivalent totrue | false
? You can trigger the same behavior when replacingboolean
with some string literal. However, if you usenumber | string
like ingg
the type ofa
becomesnumber
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 ofnumber
.The text was updated successfully, but these errors were encountered: