-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Can't use alias of string as an index signature parameter type #7374
Comments
meant to link to #2491 as well. |
@mhegazy both of those tasks you referenced as duplicates are actually quite different. #2491 is about enums (which are internally ints, not strings), and #5683 is about string literals (not aliases of string). #2491 was fixed and apparently did not fix this issue. And #5683 was solved with mapped types, which also don't apply in this case. Is there an actual issue which tracks this? Or can you link me to a thread that explains why this is a wontfix? |
There are two issues here.
For enums, our recommendation was to use mapped types, this was tracked by #2491, was fixed when |
@mhegazy I think your logic on aliases in point 1, above, is inconsistent with the rest of Typescript. Aliases for Please reconsider. |
There's already a place to put a name to communicate semantics: But type aliases -- You can alias And arguably it's strictly worse to allow aliases here, because if you see the type [k: CoolTypeAlias]: T this type has very different behavior depending on whether |
I'm strongly getting that y'all aren't going to do anything with this - but I thought I should push back regardless, in large part because of what the arguments imply for the future of the language. Our mileage differs. Unchecked typing of simple values is a common language feature, from C/C++ to Scala. I use type aliases wherever there's a domain type that I need to express. For example, in my domain most interfaces include a string ID; I type these with If I were working with physical numeric units, I'd definitely type those, having learned from all the wrong-unit megafails of the past (e.g. the Mars Orbiter).
Typing of simple values makes searching for places where a given domain type is used much easier. It would be nice if the compiler could check for correctness, but typing of numbers and strings by usage is of high value even without checking. As for not understanding whether a typed value a number or string, that's far less frequent and usually more easily detected problem than confusing the wrong domain-type, and addressable by simply clicking through to the type definition. In most cases, getting number/string wrong wouldn't even get past the compiler. |
Your argument for (e.g. units of measure are important to keep right) is essentially our argument against -- allowing type aliases as key types would create the appearance that there was nominal checking of the indexing type. In other words, it would look like you could write On the one hand you're saying "All I want is to name this key type, checking is not required", but on the other hand you're rejecting the mechanism (key names) that already does this. The phrase "unchecked typing" is sort of an oxymoron - either it's typed and some checking occurs, or it isn't (and may as well be a code comment). If you really want to name your indexing things for the purposes of finding them, you can write: interface KeyTypeAlpha<T> {
[k: string]: T;
}
interface KeyTypeBeta<T> {
[k: string]: T;
}
// Usage
interface One extends KeyTypeBeta<number> { }
interface Two extends KeyTypeAlpha<number> { } |
I don't think people would expect that behaviour. TS is structurally typed, right? You don't get that kind of typechecking with a function call so what's the difference with a key type? type AnimalId = string
type PersonId = string
const foo = (x: AnimalId) => x.slice(0, 1);
const bar: PersonId = "bob";
foo(bar); |
The compiler requires an index signature parameter to be "string" or "number" specifically, and doesn't allow type aliases.
Shouldn't CustomId be allowed there?
The text was updated successfully, but these errors were encountered: