Description
Due to unresponsiveness and a highly unsatisfactory outcome on issue #1778, I want to raise a fresh issue so I can get a clear and final outcome. I don't mind if it's another no (actually I would because it makes zero sense), but if it's a no, I would like something that wasn't provided in the referenced issue: a CLEAR solution to this problem.
So let's recap with some basics:
- There are two types of
enum
:number
, orstring
. - A type can be a
string
ornumber
, or contain a property that is astring
ornumber
- There are two types of supported index signatures:
string
andnumber
It naturally follows that index signatures should be able to be aliased, purely by observing the above facts alone.
But let's step into a practical scenario.
It's common to make the equivalent of a hashmap or lookup map, whether it be for the developer's convenience, or for performance reasons. Either way, it's almost always the case in my experience that the indexes of these hash maps correspond to a type expressed elsewhere in the codebase, and aren't just an arbitrary/random index value.
e.g.
public templateElementHashMap: { [s: string]: TemplateElement } = {};
This shows an incomplete context, as the string
type is always going to be the hash
property in the class below:
export class TemplateElement {
public hash?: string;
}
Therefore, the truest expression would be this:
public templateElementHashMap: { [s: TemplateElement['hash']]: TemplateElement } = {};
This provides full context to the developer, quick travel to the type of interest, and as long as TemplateElement['hash']
is string
or number
, the above statement is 100% correct, and does not break any rules regarding type consistency. It's a win/win, and clearly a large improvement.
Instead right now, I have many lines where the index signature is just a string
property, and it's become a pain to remember which hash map relates to which data I'm using it for. And quite often, I'm wasting time poking around trying to remember which index derives from which type.
So I'm going to ask again: what is the solution? To me there are only two:
- Make a comment after each one (zero type security, comments can be out of date, it's ugly, why bother using TypeScript if I'm trying to compensate for its shortcomings with comments everywhere)
- Get on Github and start making noise about it
I've poured a fair amount of my time providing feedback and input for TypeScript since its early days, and only ask this is treated with the promptness and attention that I have received in the past, and not closed prematurely like the other issue.
So please can I receive a clear and concise workaround, or (preferably) the acknowledgement that something actually is inadequate with the design around this and I'm not just going insane, along with the many others who have expressed the same problem.