Created
August 10, 2020 13:36
-
-
Save mattgperry/c4d23752a0fae7888596c4ff6d92733a to your computer and use it in GitHub Desktop.
useTransform tuple types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MotionValue<T> { | |
constructor(public current: T) {} | |
} | |
type UnwrapMotionValueArray<V extends MotionValue<any>[]> = { [K in keyof V ]: V[K] extends MotionValue<infer T> ? T : never } | |
function unwrap<T extends MotionValue<any>[]>(value: [...T]): UnwrapMotionValueArray<typeof value> { | |
return value.map(v => v.current) as [...UnwrapMotionValueArray<T>] | |
} | |
function useTransform<T extends MotionValue<any>[], R>( | |
values: [...T], | |
transform: (latest: UnwrapMotionValueArray<typeof values>) => R | |
): ReturnType<typeof transform> { | |
return transform(unwrap(values)) | |
} | |
const value = useTransform([new MotionValue(1), new MotionValue("hi")], ([i, s]) => i + s.charCodeAt(0) + 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would we be able to do this now? I'm digging into this but Im not the best at TS motiondivision/motion#840