We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
#26797
Today we only support string and number index signatures.
string
number
We didn't have symbol index signatures (though it used to be allowed in string index signatures)
symbol
Now we have bigint - but we don't support those either.
bigint
People asked for index signatures on enums; but instead we said you could have a mapped type over an enum.
As far as implementation goes, it is far reaching but it is reasonable.
When you index with something x[y], you have to try to apply the type of y against every index signature of x and union it to get the result.
x[y]
y
x
x[y] = z
There is some annoying behavior with these index signatures on literal types.
interface Foo { [index: "a" | "b"]: string; [index: "c" | "d"]: number; } // Error! But what's the deal! class Bar implements Foo { a = "a"; b = "b"; c = 10; d = 20; }
Bar
What about "enum-yness"
Today, a mapped type over enums just flattens down to the numeric values that an enum maps to.
But with the current PR, we keep them distinct.
enum SomeEnum { Zero, One, } interface Foo { [index: number]: number; [index: SomeEnum]: number; }
Here, we enforce that both of those index signatures are related to each other.
This also doesn't perfectly map to index signatures; you can't toggle optionality with ?.
?
If we started TypeScript over today, would we have both mapped types and index signatures?
[[Discussion around syntax and computed property names]]
"I think there's a story around index signatures for creating a set of known property names, and mapped types just being a template."
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Arbitrary Index Signatures
#26797
Today we only support
string
andnumber
index signatures.We didn't have
symbol
index signatures (though it used to be allowed instring
index signatures)Now we have
bigint
- but we don't support those either.People asked for index signatures on enums; but instead we said you could have a mapped type over an enum.
As far as implementation goes, it is far reaching but it is reasonable.
When you index with something
x[y]
, you have to try to apply the type ofy
against every index signature ofx
and union it to get the result.x[y] = z
, each type is intersected in the assignment.There is some annoying behavior with these index signatures on literal types.
Bar
hasn't declared explicit index signatures.What about "enum-yness"
Today, a mapped type over enums just flattens down to the numeric values that an enum maps to.
But with the current PR, we keep them distinct.
Here, we enforce that both of those index signatures are related to each other.
This also doesn't perfectly map to index signatures; you can't toggle optionality with
?
.If we started TypeScript over today, would we have both mapped types and index signatures?
[[Discussion around syntax and computed property names]]
"I think there's a story around index signatures for creating a set of known property names, and mapped types just being a template."
The text was updated successfully, but these errors were encountered: