Skip to content

More TypeScript updates #398

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

Merged
merged 4 commits into from
Dec 30, 2018
Merged

More TypeScript updates #398

merged 4 commits into from
Dec 30, 2018

Conversation

Jessidhia
Copy link
Contributor

Addresses what I could from #383, and adds types for useChain.

export type UseSpringProps<DS extends UseSpringBaseProps> = Merge<
DS,
export type UseSpringProps<DS extends object> = Merge<
DS & UseSpringBaseProps,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change fixes autocomplete so it shows UseSpringBaseProps' properties on autocomplete

@@ -65,6 +86,12 @@ export type UseSpringProps<DS extends UseSpringBaseProps> = Merge<
>

// there's a third value in the tuple but it's not public API (?)
export function useSpring<DS extends CSSProperties>(
values: UseSpringProps<DS & CSSProperties>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This trick lets CSSProperties show up on autocomplete by default (as long as no property that you add is incompatible with CSSProperties itself; then it falls back to the other overloads)

@@ -64,7 +64,7 @@ export interface SpringBaseProps {
onStart?(): void
}

export interface SpringProps<DS extends object = {}> {
export interface SpringProps<DS extends object = {}> extends SpringBaseProps {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this to address #386

* Prevents animation if true, you can also pass individual keys
* @default false
*/
immediate?: boolean | string[] | ((key: string) => boolean)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did immediate ever actually accept string[]? I can't find it in the code (in fact if it's an array it's just treated as always true in the Controller). This made it incompatible with SpringBaseProps so I had to remove the string[].

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kovensky No, immediate can be a boolean or a function returning boolean. This could be a left over.

@Jessidhia
Copy link
Contributor Author

As I try to actually make use of this in a real project I noticed the type definition for useTransition is almost completely wrong 😓

I also wonder if it's possible to attach .interpolate to the output types. Might be again running into the "too generic for typescript" issue.

@drcmda
Copy link
Member

drcmda commented Dec 22, 2018

transitions return Array<{ item, key, props, state }>, wheresare state can be enter|leave|update and props is an object that contains animated values (which can interpolate).

@Jessidhia
Copy link
Contributor Author

Jessidhia commented Dec 22, 2018

Yeah, the output type is (almost) correct, but also very wrong because the input type is wrong.

First, it assumes that, like useSpring, the from is derived from the input props but that seems to not be the case with useTransition. Second, it puts the input object itself (minus some filtered props) as props instead of the union of the types of from / enter / update / leave.

@drcmda
Copy link
Member

drcmda commented Dec 22, 2018

This we'll change soon, see: #393 i guess it should be useTransition(input, keys, config), just like useTrail(items, config). Can we anticipate this change already and exclude items, or will TS make it impossible to use?

@Jessidhia
Copy link
Contributor Author

Jessidhia commented Dec 22, 2018

I think it has to be done at the same time, because items is currently required to be able to infer a generic argument.

(Somewhat related: initial is not working correctly, state.current.first is still false the first time transitions is calculated so initial is only used for not initial enters; the initial: null behavior is also unsupported)

@cdebotton
Copy link
Contributor

cdebotton commented Dec 22, 2018

@Kovensky I apologize if you've already considered this approach, but I had fairly primitive types I was using for just useSpring that were working with initial release of react-spring with hooks that supported chaining interpolate. Perhaps these might be helpful?

import { SpringConfig } from 'react-spring';

declare module 'react-spring' {
  type SpringOptions<T> = {
    config?: SpringConfig | ((key: string) => SpringConfig);
  } & T;

  type SpringSetter<T> = (options: Partial<SpringOptions<T>>) => void;

  type InterpolationOptions<T, U = T> = {
    range: T[];
    output: U[];
  };

  interface InterpolationFn<T> {
    <U>(options: InterpolationOptions<T, U>): Interpolation<U> & U & string;
    (interpolator: (params: T) => string): Interpolation<T> & T & string;
  }

  type Interpolation<T> = {
    interpolate: InterpolationFn<T>;
    getValue: () => T;
  } & T;

  type AnimatedValue<T> = { [K in keyof T]: Interpolation<T[K]> };

  export function useSpring<T>(
    options: SpringOptions<T>,
  ): [AnimatedValue<T>, SpringSetter<T>];
}

The new hooks may be more complex in a way that's prohibitive of using this.

@cdebotton
Copy link
Contributor

I've actually opened up a PR that adds the typings for chains.

#400

@drcmda drcmda merged commit e8fe471 into pmndrs:master Dec 30, 2018
szjemljanoj pushed a commit to szjemljanoj/react-spring that referenced this pull request Mar 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants