You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a situation that keeps coming up. It leads to avoidable bugs, and I want to know if there is way to prevent it.
Let's say I am building a lookup object, where each key is a person's id and each value is a person object. I am building this object dynamically from an array of objects which are only known at runtime.
var peopleArray = [ // Only known at runtime
{ id: "1", name: "Tim" },
{ id: "2", name: "Tom" },
{ id: "3", name: "Ted" }
];
var people: { [id: string]: any } = {};
peopleArray.forEach(person => {
people[person.id] = person;
});
Now sometimes when not thinking, or as a result of a big refactor, I find myself accessing objects on the object with dot syntax, thinking it is another object of another type, like this...
var aThing = people.something;
something will never be a key in this object because it is only keyed by ids, but TypeScript will accept this as valid code and not raise an error because the object could be keyed by any string.
The assertion I can make here is that this object should only be accessed with square bracket syntax, not dot-syntax since it is impossible to know the values at compile time.
So, to prevent this happening, is there (or could there be) a way of the compiler raising errors for dot-syntax navigation on an object, if the programmer knows that the keys will only be known at runtime?
Something like:
var people: { indexable [id: string]: any } = {};
var aThing = people.something; // Raises an error
var aThing2 = people['something']; // Doesn't raise an error
Hope this makes sense.
Thanks in advance!
Chris
The text was updated successfully, but these errors were encountered:
ChrisBellew
changed the title
Suggestion: Allow forcing only indexed access to object values
Suggestion: Allow forcing only square bracket access to object values
Nov 23, 2017
ChrisBellew
changed the title
Suggestion: Allow forcing only square bracket access to object values
Suggestion: Allow forcing only square bracket navigation on a given object
Nov 23, 2017
The train is long gone now. This was the behaviour of TypeScript pre 2.2 but it got changed afterwards. There was a discussion in #12596 which ultimately led to this questionable feature going in.
The assertion I can make here is that this object should only be accessed with square bracket syntax, not dot-syntax since it is impossible to know the values at compile time.
There is no semantic difference between the two modes of access.. nor there is a difference at runtime. anything the compiler does here is artificial at best. please see #12596
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Hi,
I have a situation that keeps coming up. It leads to avoidable bugs, and I want to know if there is way to prevent it.
Let's say I am building a lookup object, where each key is a person's
id
and each value is a person object. I am building this object dynamically from an array of objects which are only known at runtime.Now sometimes when not thinking, or as a result of a big refactor, I find myself accessing objects on the object with dot syntax, thinking it is another object of another type, like this...
something
will never be a key in this object because it is only keyed byid
s, but TypeScript will accept this as valid code and not raise an error because the object could be keyed by any string.The assertion I can make here is that this object should only be accessed with square bracket syntax, not dot-syntax since it is impossible to know the values at compile time.
So, to prevent this happening, is there (or could there be) a way of the compiler raising errors for dot-syntax navigation on an object, if the programmer knows that the keys will only be known at runtime?
Something like:
Hope this makes sense.
Thanks in advance!
Chris
The text was updated successfully, but these errors were encountered: