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

Suggestion: Allow forcing only square bracket navigation on a given object #20240

Closed
ChrisBellew opened this issue Nov 23, 2017 · 3 comments
Closed
Labels
Question An issue which isn't directly actionable in code

Comments

@ChrisBellew
Copy link

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.

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

@ChrisBellew 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 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
@gcnew
Copy link
Contributor

gcnew commented 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.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Nov 27, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 27, 2017

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

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants