Consider this interface:
interfaceA{
foo: string;
bar: Function;}With an operator such as membersof we can have this type:
typeMembersofA=membersofA;// equivalent of type MembersofA = 'foo' | 'bar';
Some caveats:
- Deceleration merging: I think the compiler is smart enough to resolve all merged properties before building the type for
membersof. - Inheritance: If the compiler is smart enough to handle 1, this is a piece of cake 😄
- Index signatures: In case we have an interface like this:
interfaceA{
foo: string;[property: string]: any;}The type should turn into: 'foo' | string. Although I'm not so sure about this.
4. new and function signature: I think these can be ignored safely:
interfaceA{new();(bar: number): void;
foo: string;}typeMembersofA=membersofA;// 'foo'- Other type constructs:
- class: public members?
- enum: members, should be easy. maybe even easy for constant enums
- namespace: treated like interfaces
- type alias, union/intersection: doesn't make sense, does it?
There are many use cases. immutable js is the best one:
import{Map}from'immutable';interfaceMapShape{
foo: number;
bar: string;
baz: boolean;}constmap=newMap<MapShape>();map.set('blah',1);// error, unless [other: string]: any; is added to MapShapeAccompanied by #6080 we can have type safety too:
map.set('bar',1);// error: 1 is not stringMap would look like this:
classMap<T>{set(prop: membersofT,value: any): Map<T>;}
// or with "Value of string literal type as property names" (#6080)
class Map<T>{set<UextendsmembersofT>(prop: U,value: T[U]): Map<T>;}
// Although this might need to have the output of membersof T to be extensible!This is easier to implementation than #1295.
Consider this interface:
With an operator such as
membersofwe can have this type:Some caveats:
membersof.The type should turn into:
'foo' | string. Although I'm not so sure about this.4.
newand function signature: I think these can be ignored safely:There are many use cases. immutable js is the best one:
Accompanied by #6080 we can have type safety too:
Map would look like this:
This is easier to implementation than #1295.