-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Support inferred string-modified union types #17512
Comments
I believe this is roughly the same idea as proposed in #16746 (comment) I think it is a great idea, but might be highly complex. I would say it is not an edge case because, if generalized somewhat, it could allow for something like (syntax is just something I made up) interface Outer {
details: Inner
}
interface Inner {
name: string;
}
type Delimiter = ".";
type DeepKey<T, OKey extends keyof T, IKey extends keyof T[OKey]> = OKey + Delimiter + IKey;
function pick<T, OKey extends keyof T, IKey extends keyof T[OKey]>(o: T, key: DeepKey<T, OKey, IKey>) {
const [outer, inner] = key.split(".");
return o[outer][inner];
}
declare const o: Outer;
const name = pick(o, "details" + "." + "name"); |
Another related issue #12940. |
As well as #12754 |
We'd either have to invent a completely new kind of type (a deferred-expansion union), which would be a ton of work, or risk people accidently creating combinatorially explosive literal types. This doesn't seem to come up often enough to warrant either scenario. |
I ran into an interesting situation that I wanted to run by the community to see if there's a way to add a new feature into TypeScript to support. I'll grant that it's an edge-case, but it seems like something that could be kind of neat.
Here's the code:
Code
I think it would be neat if the expression:
could be automatically inferred to be of type:
It's probably a lot more complex than it seems. And there's probably a heck of a lot of issues dealing with what happens with other string operations or if a runtime function like .toUpperCase() is called on the string, and what happens if any item in the union isn't a string, etc, etc. But still.
Any thoughts?
The text was updated successfully, but these errors were encountered: