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

Proposal: string literal type manipulation in mapped types #22041

Closed
ericanderson opened this issue Feb 20, 2018 · 5 comments
Closed

Proposal: string literal type manipulation in mapped types #22041

ericanderson opened this issue Feb 20, 2018 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@ericanderson
Copy link
Contributor

ericanderson commented Feb 20, 2018

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

declare loadThings<T>(T): PossiblyLoading<T>;
declare getTheBar(id: number): Loader<Bar>;

let foo: { bar: Bar | undefined, barLoading: boolean };

foo = loadThings({
  bar: getTheBar(55302),
});

Where PossiblyLoading might be implemented like so:

type PossiblyLoading<T> = {
  [K in keyof T]: UnwrapLoader<T[K]>;
  [(K in keyof T) + "Loading"] + ?: boolean
}

Both should work because the string literal is easily detectable vs the token ?.

This should also work with generic types that extend string:

type MutateProps<T, P extends string, S extends string> = {
  [P + (K in keyof T) + S] + ?: T[K]
}

But I would also want to be able to use these within conditional types, using standard javascript functions:

type RemovePrefixed<T, P extends string> = {
  [K in keyof T]: K.startsWith(P) ? never : T[K];
}

And we could even remove a prefix:

type RemovePrefix<T, P extends string> = {
  [(K in keyof T).startsWith(P) ? K.slice(P.length) : K]: T[K];
}

If we wanted to avoid getting too crazy with function calls we could perhaps do it like this:

type RemovePrefixed<T, P extends string> = {
  [(P + infer K) in keyof T ? never : K]: T[K];
}

type RemovePrefix<T, P extends string> = {
  [(P + infer K) in keyof T ? K : never]: T[P + K];
}

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

@ericanderson
Copy link
Contributor Author

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];

@mhegazy
Copy link
Contributor

mhegazy commented Feb 20, 2018

Seems like a duplicate of #12754

@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 20, 2018
@ericanderson
Copy link
Contributor Author

Damn, didnt see that one. I think this provides alternate suggestions, but it is very similar.

@ericanderson
Copy link
Contributor Author

Yeah, core differences: I expect the string addition to be within the [] and propose use of string functions like slice and indexOf etc.

@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

3 participants