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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
With this PR we implement support for symbol and template literal string index signatures. We furthermore permit index signature declarations to specify union key types, provided all constituents are either string, number, symbol, or template literal types with non-generic placeholders. Some examples:
An index signature declaration that specifies a union key type is exactly equivalent to a set of distinct index signatures for each constituent key. For example, the PropertyMap declaration above is exactly equivalent to:
Index signature declarations are not permitted to specify literal key types or generic key types. Those kinds of types can only be used with mapped types, which map literals key types to distinct properties and defer resolution of generic key types until they're instantiated with non-generic types. For example:
This PR supercedes #26797 which was more ambitious but had overlap between regular properties and index signatures with literal key types that is difficult to reconcile, as well as generic index signatures for which type relationships become exceedingly complex to reason about.
Something is amiss with this, or it's not been released yet. Still receiving the 'ol error TS1337: An index signature parameter type cannot be a union type. Consider using a mapped object type instead. errors. @RyanCavanaugh can you confirm it's been released as of 4.3.5?
Something is amiss with this, or it's not been released yet. Still receiving the 'ol error TS1337: An index signature parameter type cannot be a union type. Consider using a mapped object type instead. errors. @RyanCavanaugh can you confirm it's been released as of 4.3.5?
It's currently in the 4.4 release which is in beta scheduled to be released by the end of August.
@ahejlsberg Could you please clarify why those cases work differently in terms of assignability (playground)? AFAIU for a type alias and anonymous type it uses an empty literal type from getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode without resolving its members, but is it the expected behavior?
const sym = Symbol();
function gg3(x: { [key: string]: string }, z: { [sym]: number }) {
x = z; // Ok
}
interface GG4 {
[sym]: number;
}
function gg4(x: { [key: string]: string }, z: GG4) {
x = z; // Error
}
type GG5 = {
[sym]: number;
}
function gg5(x: { [key: string]: string }, z: GG5) {
x = z; // Ok ?!
}
@ahejlsberg - This is really a great feature, thanks!
I noticed that spaces seem to count as digits with the number type. How about not allowing that?
Of course with unquoted literal labels it doesn't matter, but with quoted string indices it does.
type TP4 = {[k:`${number}`]:null}
const tp4_1:TP4 = {'1':null}
const tp4_2:TP4 = {' ':null} // even though there is no number present, it passes
const tp4_3:TP4 = {' 1':null}
const tp4_4:TP4 = {'1 ':null}
const tp4_5:TP4 = {' 1 ':null}
If there're other keys needing restricting value type, how should I put it?
type Test = 'one' | 'two';
type Foo= {
stringKey: string; // A mapped type may not declare properties or methods.
numberKey: number;
[key in Test]: any;
}
enum Test {
ONE = 'one',
TWO = 'two'
}
interface Foo {
[key: Test]: any; // An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead.
}
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With this PR we implement support for symbol and template literal string index signatures. We furthermore permit index signature declarations to specify union key types, provided all constituents are either
string
,number
,symbol
, or template literal types with non-generic placeholders. Some examples:An index signature declaration that specifies a union key type is exactly equivalent to a set of distinct index signatures for each constituent key. For example, the
PropertyMap
declaration above is exactly equivalent to:Index signature declarations are not permitted to specify literal key types or generic key types. Those kinds of types can only be used with mapped types, which map literals key types to distinct properties and defer resolution of generic key types until they're instantiated with non-generic types. For example:
This PR supercedes #26797 which was more ambitious but had overlap between regular properties and index signatures with literal key types that is difficult to reconcile, as well as generic index signatures for which type relationships become exceedingly complex to reason about.
Fixes #1863.
Fixes #26470.
Fixes #42192.
Fixes #44675.