-
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
Index signatures for symbols and template literal strings #44512
Conversation
Something is amiss with this, or it's not been released yet. Still receiving the 'ol |
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
|
Is this in and working? I'm sitting on
type Test = 'one' | 'two';
export interface Foo {
[key: Test]: any;
} |
You can do it using type Test = 'one' | 'two';
type Foo= {
[key in Test]: any;
}
//type Foo = {
// one: any;
// two: any;
//}
type Foo2= {
[key in `${Test}${Test}`]: any;
}
//type Foo2 = {
// oneone: any;
// onetwo: any;
// twoone: any;
// twotwo: any;
//} I believe that because there is no "free" string or number template, this is old hat, and not related to this new feature. |
@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?
|
Index signatures for template literal string with generic? type Props<T extends string> = {
renders: {[key in T]: any};
[key: `render${Capitalize<T>}`]: any;
}; TS 4.5.2 says:
|
|
|
4.4.2 was the 1st npm release to allow template string pattern index signatures, used in this PR. see microsoft/TypeScript#44512
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.