-
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
Proposal: string literal type manipulation in mapped types #22041
Comments
Assuming recursive types: const object = { 'a': { 'b': { 'c': 3 } } };
let foo: number = getAt(object, 'a.b.c');
type IndexedAt<O, S extends string> = S.indexOf('.') == -1 ? O[S] : IndexedAt<S.slice(S.indexOf('.')), O[S.slice(0, S.indexOf('.'))]> Or without recursive types: type IndexedAt<O, K extends (A + "." + B + "." + C)> = O[A][B][C]; |
Seems like a duplicate of #12754 |
Damn, didnt see that one. I think this provides alternate suggestions, but it is very similar. |
Yeah, core differences: I expect the string addition to be within the |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
I would like the ability to do string manipulation within my types. Examples could include stripping out properties that match a regex/prefix/suffix or mapping types to include additional properties derived from the keys of existing ones.
Some examples might explain best:
Example
Where
PossiblyLoading
might be implemented like so:Both should work because the string literal is easily detectable vs the token
?
.This should also work with generic types that extend string:
But I would also want to be able to use these within conditional types, using standard javascript functions:
And we could even remove a prefix:
If we wanted to avoid getting too crazy with function calls we could perhaps do it like this:
Although I think the one that supports a subset of string function is more broadly useful. For example, we could probably do something around indexing into "foo.bar.baz" and resolve the innermost type of baz.
cc/ @ahejlsberg @mhegazy
The text was updated successfully, but these errors were encountered: