It would be useful if we could combine string literal types to create new types. For example suppose A and B extend string then:
type AB = A + B; // strings formed from a member of A concatenated with a member of B.
type Ax = A + string; // strings starting with a member of A
type xB = string + B; // strings ending with a member of B
This would combine well with mapped types. For example suppose we want to specify an ordering of records of type T to some third-party module that requires SQL-like syntax. We could write:
function orderBy<T>(records: Array<T>, order: (keyof T) + ('' | ' ASC' | ' DESC')) {
...
}
let records = [{x: 5}, {x: 2}, {x: 7}];
orderBy(records , 'x');
orderBy(records , 'x DESC');
Maybe some other forms would be useful:
type notA = !A; // any string except for the members of A
type notAx = !A + string; // strings not starting with a member of A
type AxB = A + string + B; // equal to Ax & xB
type xAx = string + A + string; // strings containing a member of A
type A3 = A * 3; // A + A + A
Should be covered by the proposal in https://github.com/Microsoft/TypeScript/issues/6579
@mhegazy I think this it depends how deep we want to go with this. Regex types are a much more generalized idea, but also bring complexity.
Also it seems that Regexes are at odds with keyof. Literal concatenation (i.e. only string literals + keyof) sounds like a good compromise.
It's not a duplicate of #6579 without some way of incorporating a keyof into a regexp, which would seem to be a can of worms.
The sticking point seems to be the use of non-enumerable types extending string apart from string itself. In mapped types, only: {[P in string]: ...} is supported but this is identical to {[key: string]: ...}.
The proposal for A + B above would take two enumerable types and create a third, so it would be usable without any further changes and fairly useful as explained in the example.
then https://github.com/Microsoft/TypeScript/issues/12754 is what you are looking for.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Most helpful comment
then https://github.com/Microsoft/TypeScript/issues/12754 is what you are looking for.