Skip to content

Instantly share code, notes, and snippets.

@mattgperry
Created August 10, 2020 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattgperry/c4d23752a0fae7888596c4ff6d92733a to your computer and use it in GitHub Desktop.
Save mattgperry/c4d23752a0fae7888596c4ff6d92733a to your computer and use it in GitHub Desktop.
useTransform tuple types
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)
@huntercaron
Copy link

huntercaron commented Nov 12, 2020

Would we be able to do this now? I'm digging into this but Im not the best at TS framer/motion#840

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment