-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
ObjectConstructor.values returns any[] in at least some cases when used in a template function #55116
Comments
FYI, |
@MartinJohns - Good reference, thanks. As you indirectly state,
that it should return the specific non- |
I'm just saying that the safe approach is for Take this example: const orig = { str: 'string', num: 123 };
// Works, because the types are compatible. All types required by "reduced" are present and compatible.
const reduced: { str: string } = orig;
// Works, because the information about the property "num" is lost when the object was assigned to "reduced".
const record: Record<string, string> = reduced;
// According to the type "Record<string, string>" all values should be types string, right?
// Due to the wrong / unfortunate implementation it actually is typed string[]!
const values = Object.values(record);
// But there's actually a number present!
console.log(values); |
This isn't a bug in TypeScript, is it? TS intentionally refuses to synthesize union types for inference candidates, because otherwise just about everything would succeed where people might expect a failure (canonical example is If I interpret this as a feature request for |
I do see your point. Although I think the "blame" could equally be assigned to allowing There are also disadvantages of
So while I certainly see your point, I think it is a matter of tradeoffs rather than an absolute. |
That's how TypeScript operates fundamentally. It's a structurally typed language. To error or warn here would be just wrong. But otherwise, yeah, nowadays |
@jcalz - You are correct - I am not talking about changing the behavior of Typescript inference. I am only talking about changing the type definition of Also - I must apologize - my statement of the problem was not clear, had errors, and I rewrote it. Does it make any difference to what you are saying? If you are saying there are other cases of .d.ts type definitions for the js core functions and they all need to share the same policy wrt to union types, that sounds reasonable, but your |
I don't really want to poke this hornet's nest. |
Closing |
Bug Report
π Search Terms
ObjectConstructor
π Version & Regression Information
β― Playground Link
Playground
π» Code
π Actual behavior
For the union
A|B
caseObject.values
returnsany[]
π Expected behavior
For the union
A|B
caseObject.values
returnsnumber[]|boolean[]
.Object.values(null as any as A)
number[]
number[]
Object.values(null as any as B)
boolean[]
boolean[]
Object.values(null as any as (A|B))
any[]
number[]|boolean[]
The text was updated successfully, but these errors were encountered: