-
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: support pathof along with current keyof #20423
Comments
So, if I understand correctly, the idea is that you could have something like var m: TypedMap<{ a: { b: number } }>;
var m2 = m.set('a.b', 42); where You can already have type safety for a similar function m.set('a', 'b', 42); with the following type definition for interface TypedMap<T> {
get<K extends keyof T>(k: K): T[K];
get<K extends keyof T, K2 extends keyof T[K]>(k: K, k2: K2): T[K][K2];
get<K extends keyof T, K2 extends keyof T[K], K3 extends keyof T[K][K2]>(k: K, k2: K2, k3: K3): T[K][K2][K3];
set<K extends keyof T>(k:K, v:T[K]): TypedMap<T>;
set<K extends keyof T, K2 extends keyof T[K]>(k:K, k2: K2, v:T[K][K2]): TypedMap<T>;
set<K extends keyof T, K2 extends keyof T[K], K3 extends keyof T[K][K2]>(k:K, k2: K2, v:T[K][K2][K3]): TypedMap<T>;
} Now, this approach does have a few drawbacks, of course; the API isn't quite as neat and the supported depth of the objects is limited by the number of overloaded function definitions you are willing to write. Still, most of the use cases you mention could be achieved with the current state of the type system. |
One problem is that the following is completely legal: interface A {
a: boolean;
b: 2;
'a.b': string;
'"a.b"': number;
}
type T1 = A['a']; // boolean
type T2 = A['a.b']; // string
type T3 = A["'a.b'"]; // string[]
type T4 = A['"a.b"']; // number In fact, any path string you can possibly come up with is a fully legal |
@noppa Thank you for contributing your thoughts yes the above is another way to approach this issue but as you mentioned, it has the depth limitation but also a productivity* issue(if we manually have to do all the overloading or rewrite typings for every type since depth is variable). type PathSetter<T, K extends keyof T ,P=K,M=TypedMap<T>> = ( p:P, t: T[keyof T]) => M | PathSetter<T[K], keyof T[K], [P, ...keyof T[K]], M>
export type TypedMap<T> = {
get: <K extends keyof T >(k:K) => T[K];
set: <K extends keyof T >(k:K, v:T[K]) => TypedMap<T>;
setIn: PathedSetter<T, keyof T>;
} which won't work since I can't use spread operator on keys |
My example would also work with tuple types, which might be closer to what you are looking for interface TypedMap<T> {
set<K extends keyof T>(k:K, v:T[K]): TypedMap<T>;
set<K extends keyof T, K2 extends keyof T[K]>(k: [K, K2], v:T[K][K2]): TypedMap<T>;
set<K extends keyof T, K2 extends keyof T[K], K3 extends keyof T[K][K2]>(k: [K, K2, K3], v:T[K][K2][K3]): TypedMap<T>;
}
declare var m: TypedMap<{ a: { b: number } }>;
var m2 = m.set(['a', 'b'], 42); |
Yes that's true but it should happen in a recursive way that's what I tried in my example above but since we cannot have spread operator on the array, it's not doable I guess. |
I think a possible solution could be blocked by: |
In addition to the libraries mentioned in the initial comment, something like this proposal is absolutely necessary in order to provide any sort of type checking on queries in the The other very common use case I keep stumbling on is dot notation in query string parameters. |
This is an eagerly awaited feature for me! It would allow for much less headaches when dealing with utility functions designed to pull nested properties out of other types! |
Just implemented this feature const c = { z: { y: { bb: 123 }}};
const path = pathOf(c, 'z', 'y', 'bb');
// path now is typeof [ 'z', 'y', 'bb' ]
const path2 = pathOf(c, 'z', 'y', 'gg'); // error, because no 'gg' field in c.z.y Also types only approach: let path: pathOf3<typeof c, 'z', 'y', 'bb'>;
path = pathOf(c, 'z', 'y', 'bb'); |
Thanks for posting in the other thread, @Morglod. For reference to others, #12290 also discusses tackling similar functions. fwiw, tackling I feel this problem is more general than just path-based navigation though -- similar challenges exist for lenses/traversals as well. Given that, I would advocate a more generic approach based on recursion, which I hope will gain further support. @agalazis: I think #17884 actually ended up superseded by a recently merged PR by ahejlsberg! :) |
@tycho01 was it released? looking forward to giving it a shot |
@tycho01 just updated ts-pathof. Now you can: import { hasPath } from 'ts-pathof';
const c = { z: { y: { bb: 123 }}};
const path = hasPath(c, [ 'z', 'y', 'bb' ]);
path -> [ 'z', 'y', 'bb' ]
const path2 = hasPath(c, [ 'z', 'y', 'gg' ]); // no error
path2 -> value is false, type is never or import { PathOf } from 'ts-pathof';
const o = {x: { y: 10 }};
type xy = PathOf<typeof o, ['x', 'y']>;
xy -> ['x', 'y']
type xyz = PathOf<typeof o, ['x', 'y', 'z']>;
xyz -> never |
@agalazis: check out #24897. while technically it doesn't go as far, |
@Morglod still you need 277 lines of code to achieve what we could achieve in just a few (in my example) if spreading keyOf was supported (and to be fair I am not sure how your code copes with arbitrarily nested objects or if it has some sort of limit) |
@agalazis Actually you are right, this hack works only for 2 levels deep. |
@Morglod Your work is amazing(I would use your library any time), my point was that the proposal is still valid, I am sure you agree |
@agalazis ye it should be in language |
This would solve a lot of problems. |
we do it like this: (in a class query)
so we could use like this:
so we could convert our c# code to typescript nearly the same... |
Any news or related thread? That's really problematic in some kind of libraries, such as MongoDB. Where dot notation is used to atomically change fields on a document. Also on Lodash get/set, ramda and various other libraries. |
I've been working on a solution to this problem using template string types. Playground link It's a little convoluted but I was trying to avoid full-on recursion in hopes of suffering from less of a performance penalty. I think it works but may still be slow. I asked on the TS Gitter and was given a much simpler solution by user type PathOf<T, K extends string, P extends string = ""> =
K extends `${infer U}.${infer V}`
? U extends keyof T ? PathOf<T[U], V, `${P}${U}.`> : `${P}${keyof T & (string | number)}`
: K extends keyof T ? `${P}${K}` : `${P}${keyof T & (string | number)}`;
declare function consumer<K extends string>(path: PathOf<KnownObjectType, K>); Note that the second generic parameter is the path string itself, so explicitly passing it is basically redundant -- I think the pros and cons between my original solution come down to capabilities and performance. My solution fully computes four-deep paths, which I'm sure is slow -- possibly awful if the input type has lots of index signatures etc. Webstrand's solution requires partially-specified generic params to be useful, which is always kind of a hassle, but likely performs a lot better because it's not forcing the compiler to compute the full set of possible key names. (As an aside, I'd still love to see an officially supported solution for this problem.) |
In case anybody is tracking this, here's a refined type StringableKey<T> =
T extends readonly unknown[]
? number extends T['length']
? number : `${number}`
: string | number;
type Path<T> =
T extends object
? {
[P in keyof T & StringableKey<T>]: `${P}` | `${P}.${Path<T[P]>}`;
}[keyof T & StringableKey<T>]
: never; Like the other one, I would expect this to perform poorly when used on a very deep or complex input type, and to error out completely if the type is recursive. |
@thw0rted I didn't know about template string types, when was it introduced? Can it be used as a discriminant property in a tagged Union type? |
It's a relatively new feature. I haven't tried it with tagged unions. I think in most cases it's supposed to work pretty similarly to a literal union, i.e. I played with it a little and it looks like a pretty good result. You can try it for yourself if you like. |
In case anybody tracking this issue is curious, I asked for a built-in helper type in #46337 and got shot down because helper types can be kind of controversial. As I said over there, maybe a keyword would be better in the long run since the team could really think about how to make it performant, how to handle recursion, how to improve error messages, etc etc. Still watching with interest! |
Problem - What are users having difficulty with?
While using popular libraries for their day to day development needs, developers can find them disjoint from typesafe philosophy even if they do have the needed typings. This is true for frequently used libraries such as lodash and immutablejs that access or set properties via paths. It is up to the developers to to preserve typesafety by expressing complex types using the amazing power that typescript gives them. The only obstacle is the absence of a way to express the type that represnts legit object paths for a specific type.
Current state
We can currently do this only for shallow objects where paths can be simply expressed as the keys of a specific type.
Example Issue
In an effort to play with stricter typings for immutablejs map I created this tiny project :
https://github.com/agalazis/typed-map/
(as a side note my personal view is that immutable js map is not a conventional map that should be represented with map<keyType, valueType> since it represents a map that matches a specific type rather than just a key,value data structure as demonstarated in
src/examples
)The type I created was just this (only playing with get and set):
Simple enough, leveraging all the expressiveness of typescript.
Example usage of the proposed solution:
If I could replace keyof with pathof in order to express possible path strings and also used T[P] as the path type I would be able to completely cover this use-case :
Possible usage in the following libraries:
Why bother since this does not involve facilitating any feature of ES standard?(and is somewhat an unconventional feature)
Alternatives
While digging further into the issue an alternative solution is to be able to spread keyof recursively. This will allow us to be creative and build our own solution as per:
#20423 (comment)
The drawbacks of the alternative solution if doable are:
Implementation Suggestions
The optimal (if implemented in the language) would be to not compute the full nested object paths but only the paths used via pathof ie when declaring something as path of x just accept it as pathof x then when a value is assigned just validate it is pathOf x( the compiler could also have some sort of caching so that it doesn't recalculate paths).
This will also solve the issue with cyclic references since paths will be finite anw.
The text was updated successfully, but these errors were encountered: