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
Object.freeze overloads force the type to be recognized as an array (by the order of overloads) #49149
Comments
That seems to be an unusual development workflow though. Most of the time you only need to specify |
@Josh-Cena I have a type SocialNetwork = { name: string ; url: string; } I need this type to be a kinda centric type and reusable. So in different places I can have different functions that produce this structure: function getFacebook() {
return Object.freeze<SocialNetwork>({});
} when I write the function above, I expect my IDE to help me completing the object with suggesting required keywords. And later: function getFacebook() {
return Object.freeze<SocialNetwork>({name: 'Facebook', url: 'https://facebook.com/my-page' });
} when I already have function above, I want any change in future to the actual |
I see. That makes sense. |
@MartinJohns thanks In the above screenshot you attached, the Also, from clean-code point of view and architecture point-of-view what you say is basically right, but the The reason I opened this issue is to solve more complicated problems, e.g.: type SocialNetwork = {
url: string;
};
type CommonProfileHandle = {
handle: string;
};
function getSocial(): SocialNetwork | CommonProfileHandle | null {
if (someConditionIsCorrect) {
return Object.freeze({
/* here it is not deterministic */
});
} else if (someOtherConditionIsCorrect) {
return Object.freeze({
/* here it is not deterministic */
});
}
return null;
} Update Update |
I think it probably makes sense to move overloads with no constraints on the type parameter after overloads that do have constraints. But even then, you can’t disambiguate a call with explicit type arguments between Accepting a PR so we can try this out and run extended tests to see if real-world usage breaks. No guarantee it will be mergeable. |
Thanks @andrewbranch . Great news. I will try to see if I can understand this whole TypeScript thing and make a change. (this does not mean I am reserving this issue to work on this) |
I missed this part of the suggestion by the way, but unless I’m missing something, I think you’re right. The array-to-readonly-array overload seems unnecessary, as it’s covered by |
lib Update Request
Configuration Check
My compilation target is
es5
and my lib is["dom", "dom.iterable", "esnext"]
.(Which I believe does not matter in this context as the definition of
freeze
is insidelib.es5.d.ts
solely.)Missing / Incorrect Definition
Object.freeze
It is defined in a way that in IDEs it is always supposed the input is an array.
Sample Code
Documentation Link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
I believe the overload that forces arrays is defined in this PR:
https://github.com/microsoft/TypeScript/pull/12434/files
And I believe just removing it will suffice (and the user of Object.freeze needs to specify its an array in case its an array)
If it is confirmed, I would be happy to create the PR for it.
The text was updated successfully, but these errors were encountered: