Skip to content
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

String literal type operators #12940

Closed
zamb3zi opened this issue Dec 15, 2016 · 6 comments
Closed

String literal type operators #12940

zamb3zi opened this issue Dec 15, 2016 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@zamb3zi
Copy link

zamb3zi commented Dec 15, 2016

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
@mhegazy
Copy link
Contributor

mhegazy commented Dec 15, 2016

Should be covered by the proposal in #6579

@mhegazy mhegazy added the Duplicate An existing issue was already created label Dec 15, 2016
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Dec 15, 2016

@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.

@gcnew
Copy link
Contributor

gcnew commented Dec 15, 2016

Also it seems that Regexes are at odds with keyof. Literal concatenation (i.e. only string literals + keyof) sounds like a good compromise.

@zamb3zi
Copy link
Author

zamb3zi commented Dec 16, 2016

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.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 16, 2016

then #12754 is what you are looking for.

@DanielRosenwasser DanielRosenwasser added Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined Suggestion An idea for TypeScript and removed Duplicate An existing issue was already created labels Aug 3, 2017
@mhegazy mhegazy added Duplicate An existing issue was already created and removed Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined Suggestion An idea for TypeScript labels Feb 20, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants