(This seems to have some things in common with #6080 and is strongly related to #1295 as it can be used to provide an alternative solution to it but l opened this separately since I did not feel it was appropriate to continue discussing it there and wanted to approach it in a more generalized way)
This would provide improved type safety for cases where an interface property is referenced through a string having a value that's known at compile time:
typeMyType={"ABC": number};letx: MyType={ABC: 42};constpropName="ABC";// 'propName' received the literal type "ABC" (not string)lety=x[propName];// 'y' received the type MyType["ABC"] which resolved to 'number'And with support for generic types added, this could work similarly, but resolve to an intermediate type of the form T[S] where T is an object type and S is a string literal type:
functionfunc<Textendsobject>(){letx: T;constpropName="ABC";// 'propName' received the literal type "ABC"lety=x[propName];// 'y' would receive the type T["ABC"]}This could combine with readonly function parameters to provide an alternative solution for #1295 (which was actually where the idea was initially proposed):
functiongetProperty<Textendsobject,Sextendsstring>(obj: T,readonlypropName: S): T[S]{returnobj[propName];}// Here T resolves to { ABC: number }, S resolves to the literal type "ABC", // and T[S] resolves to numberletx=getProperty({ABC: 42},"ABC");// Type of 'x' is number// Here T resolves to { ABC: number }, S resolves to the literal type "CBA" // and T[S] resolves to anyletx=getProperty({ABC: 42},"CBA");// Type of 'x' is any// Here T resolves to { ABC: number }, S resolves to string// and T[S] resolves to anyletx=getProperty({ABC: 42},getRandomString());// Type of 'x' is anyThis may similarly extend to numeric literal types, for tuples:
typeMyTupleType=[number,string,boolean];letx: MyTupleType;constindex=1;// 'index' received the literal type 1;lety=x[index];// 'y' received the type MyTupleType[1] which resolved to 'string'
And work with generic tuples as well:
functiongetTupleElement<TextendsArray<any>,Nextendsnumber>(tuple: T,readonlyindex: N): T[N]{returntuple[index];}Same can be done with symbol literal types (haven't thought about that much though):
functiongetSymbolProperty<Textendsobject,Sextendssymbol>(obj: T,readonlysym: S): T[S]{returnobj[sym];}
(This seems to have some things in common with #6080 and is strongly related to #1295 as it can be used to provide an alternative solution to it but l opened this separately since I did not feel it was appropriate to continue discussing it there and wanted to approach it in a more generalized way)
This would provide improved type safety for cases where an interface property is referenced through a string having a value that's known at compile time:
And with support for generic types added, this could work similarly, but resolve to an intermediate type of the form
T[S]whereTis an object type andSis a string literal type:This could combine with
readonlyfunction parameters to provide an alternative solution for #1295 (which was actually where the idea was initially proposed):This may similarly extend to numeric literal types, for tuples:
And work with generic tuples as well:
Same can be done with symbol literal types (haven't thought about that much though):