-
Notifications
You must be signed in to change notification settings - Fork 28
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
Type safe JsonPointer.get #52
Comments
Updated version that allows for number indexed types (like array)
|
Finalized version. This one can handle decoding as well. Also cleaned up the types //This is a big utility type to let us path walk a type with a string
export type SplitPath<T extends string> = T extends `${infer Prop}/${infer Rest}` ? [Prop, Rest] : [unknown, T]
//Deal with json pointer encoding
export type Decode<T extends string> =
T extends `${infer A}~0${infer B}` ? `${Decode<A>}~${Decode<B>}` :
T extends `${infer A}~1${infer B}` ? `${Decode<A>}/${Decode<B>}` :
T
export type ExtractProp<Value, Prop extends string> =
//try to see if prop is a number
Prop extends `${number}` ?
number extends keyof Value ? Value[number] :
unknown :
//check if prop is in value
Decode<Prop> extends keyof Value ? Value[Decode<Prop>] : unknown
export type PathExtract<Value, Path extends string> =
Extract<Value, SplitPath<Path>[0], SplitPath<Path>[1]>
export type Extract<Value, Prop, Rest extends string> =
Prop extends string ? PathExtract<ExtractProp<Value, Prop>, Rest> :
ExtractProp<Value, Rest> |
amazing job! so we have template literals now in TS? been waiting for soo long! |
wow this is cool! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made this type because I wanted a type safe way to pull properties from an object via a path, and I thought it might be useful to this library. I chose to make any properties not found in the object never, but you could use unknown to keep current functionality if you wanted. Just thought I'd share.
This currently does not cover ~0 and ~1 but i think with a little more work that could be figured out.
The text was updated successfully, but these errors were encountered: