-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Feature Request: Typescript Support #26
Comments
I have a hard time finding time to write a good and complete type definition for this project soon but i will definitely let you know if or when i have the time to go through the type definition :-) This might be useful in order to write the typings :-) https://github.com/drcmda/react-spring/blob/master/API.md |
I welcome PRs. I have little experience in TS myself and found it hard to wrap my head around. If you find the time to create typings, that would be awesome. 😀 |
I'm currently evaluating the transition from |
Should this issue be reopened? Due to the fact that the typing is only half finished, and ts compiler will scream when you use components other than |
Sure. |
@nelsliu9121 PR welcomed! |
Hey everyone, I've tried to add the missing types. I'm relatively new to TypeScript and to react-spring thus I'm sure I did something wrong. Basically I was just copy & pasted the stuff I have found in the initial It would be great if I can get some advice/help on how to test the annotations before I do a pull request. |
@radudascaluntt no worries, you can just create a PR! |
Will make the PR from another account. I forgot I was using this one yesterday :) |
Hi @radudascaluntt I wrote the current You can use I first edited the Most of the time you just need to follow the Hope this helps. |
I would imagine that keeping the declaration file up to date is going to become tedious in the long run... |
For the time being I've found that adding the path to {
"paths": {
// ...
"react-spring": ["./node_modules/react-spring/src/index.js"], // Not ready yet
},
// ...
} |
@raspo could you make a PR and add it to the build? TS support is supper spotty it seems and the current PRs have all fallen asleep. |
Providing only the Spring component in typings.d.ts is not a good idea. |
@jariz thanks, very happy about getting help here - i'll merge it |
I've been working on filling out typings for this, but I've ran into an issue where typescript stops being able to infer the render argument type as soon as declare class AnimatedValue<T> {
interpolate<U>(map: (value: T) => U): AnimatedValue<U>;
}
type AnimatedValueMap<T> = { [N in keyof T]: AnimatedValue<T[N]>; };
declare const animated: // distractingly big, maps JSX.IntrinsicElements to ComponentType<> with props that take AnimatedValue
type RenderPropsBase<T> = {
render: (value: T) => React.ReactNode;
}; // | { children: ... }, etc...
type RenderProps<T> =
| RenderPropsBase<T> & { native?: false }
| RenderPropsBase<AnimatedValueMap<T>> & { native: true }
;
type TransitionProps<T> = RenderProps<T> & { from?: T; enter?: T; leave?: T; };
declare class Transition<T> extends React.PureComponent<TransitionProps<T>> { }
const dynamicRenderElement = (
<Transition
from={{ x: 0 }}
enter={{ x: 100 }}
render={props => ( // Parameter 'props' implicitly has an 'any' type.
<div
style={{ left: props.x }}
/>
)}
/>
)
const nativeRenderElement = (
<Transition
native
from={{ x: 0 }}
enter={{ x: 100 }}
render={props => ( // Parameter 'props' implicitly has an 'any' type.
<animated.div
style={props.x.interpolate(left => ({ left }))}
/>
)}
/>
) If either option in The existing typings seem to "solve" this by removing any type association between the provided values ( I'll keep poking at this, but would it make sense from the JS side to offer, e.g. |
@simonbuchan i see, but is there no other way? Can't props be either an object of values and strings or an object of animatedValues? This would cause some confusion otherwise. I wanted the api to stay simple, where "native" wouldn't be that different from the default. Making additional forward-components isn't the problem: const NativeSpring = props => class extends Component {
static propTypes = {
// native type declarations here
}
render() {
return <Spring native {...props} />
}
} But it would create unnecessary overhead. PS. you only need to interpolate values if you change their structure, your example interpolates x, but it could simply be passed as always: render={props => (
<animated.div style={{ left: props.x }} />
)} |
Nope, then you can't use Regarding |
It depends, web (the default) can animate element styles and attributes (svg paths would be attributes, for instance: |
Right, |
Yeah 😱Does TS run in CodeSandBox? If we re-create your snippet above to demonstrate and isolate the problem, maybe it helps showing it around to get some input. If nothing helps we could make a typescript target with forward components, it would be easy to define, the build chain is already equipped for it. Then you would have two components for each primitive, Spring and NativeSpring, etc. each with correct types. You'd import it like so: import { NativeSpring } from 'react-spring/dist/web-ts' Or something like that. At least it wouldn't disturb plain javascript users, though i would have no idea how to document that without causing one hell of confusion. |
Since it's just a typing issue, the standard is the playground for repro issues, e.g. a TS bug report, but I would like to reduce it a bit more first. What I've got so far now implies that TS doesn't know how to contextually type the JSX props from the component signature, and it works without the union since the non-contextual inference still works, but that doesn't seem to make sense with the parameter being contextually typed. 😵 I could throw up what I've got tomorrow (9:00 PM here 😉) for this side, though, if anyone wants to give it a stab. |
You must check |
@simonbuchan i'm a total beginner when it comes to TS, but does that mean it can be solved without having to duplicate all primitives (NativeSpring, etc.) ? |
I mean, what I've got will work fine so long as you provide the type for the render argument, the win over the current types being that it can check if the values are compatible - but that's not really up to my expectations (you shouldn't have to read docs on how to use the typings for a library). Typescript already has all the information to infer everything itself, and can figure out similar, but simpler cases like in the example above, so I think this is either just a bug, or an improvement to TS they would be happy to add. The suggestion for |
The additional payload for native and animated.element isn't that big. If size is really an issue there would be more efficient ways, for instance react-spring/dist/universal (doesn't contain native, no colors) or react-spring-numerical (can be native, but no interpolations, just numbers => 2kb). |
I see that in |
Thanks for the work on the missing type definitions. The imperative API still seems to be missing types. |
I presume this is related to this ticket, TSLint is giving me this error for Transition and Spring components:
|
Scratch that. All I had to do was upgrade the React and ReactDOM types to
|
@mbeauchamp7 looks like it's an old definition. Do you know your way around TS? I welcome PR's. There are 3 statics for Spring, Trail & Transition (+ static create for custom primitives), The 3 statics accept either a function or an object https://github.com/drcmda/react-spring/blob/master/src/Keyframes.js#L80-L90 |
@drcmda I'm not very good with definition files but I can give it a try! |
Someone just updated typing, i'll try to get some more fixes in and will release soon. |
Very cool @drcmda ! Full typings would be so cool. |
@lasseborly I think they have been updated, but not released on npm |
Right, i just updated ... |
Ok.. I think I have managed to track my problem down. Limited experience with both react-spring and Typescript might make me come across as ignorant. But here we go. If I try to go by the docs and make use of <Transition
from={{ opacity: 0 }}
enter={{ opacity: 1 }}
leave={{ opacity: 0 }}
>
{loaded && Welcome}
</Transition> Usage like that resolves in an error: <Transition
from={{ opacity: 0 }}
enter={{ opacity: 1 }}
leave={{ opacity: 0 }}
>
{loaded &&
(styles => (
<Welcome style={styles}>Hello world {styles.opacity}</Welcome>
))}
</Transition> Until we venture into Typescript land where the interface of the If I'm not misunderstanding the problem and I'm not doing something fundamentally wrong you guys can correct me. If not a proposal for a PR I'll gladly produce sounds as follow.
Again, I'll gladly put up the PR. Really love this library and would be grateful for the opportunity to contribute. Even if it's "only" typings. 😊 |
Could anyone help fixing types for 6.0? Transition and Trail have changed a little. They accept a single function child: (item, state, index) => props => ReactNode item is the individual item, transiton and trail are driven by the state can be "enter"|"update"|"leave" index is the index of the item http://react-spring.surge.sh/transition Right now the types declare SpringRendererFunc, which is wrong i guess ... |
Has anyone managed to use react-spring with typescript? I tried doing this:
and got this error:
Why is items required? The type definitions seem to be totally off. What am i missing? (i'm using v 6.4.1 or react-slick) |
Also Transition`s child needs to be a function, see the docs http://react-spring.surge.sh/transition |
@pollen8 Thanks for the help! You pointed me in the right direction (with the help of this comment) and i resolved the type errors, but atm the route changes are not animated. Any idea why? Here's what i ended up with:
Not only the route transitions not animated, but CSS animations i have don't work. Scrolling is super laggy and when i open one of my routes i get this error:
And i'm not using refs anywhere in any of the components on that route. |
With the latest version of react-spring (with hooks) and using react-konva: import { useSpring, animated } from 'react-spring/konva'; I get the following error: TS7016: Could not find a declaration file for module 'react-spring/konva'.
'/.../node_modules/react-spring/konva.js' implicitly has an 'any' type. Any tip on how I can fix this? |
The module exists: https://unpkg.com/react-spring@8.0.10/konva.js But konva doesn't have types. |
Hi,
This is kind of ground breaking. I think this is er very easy and well thought abstraction for animating stuff based on state. I will definitely take a closer look into this tool :-)
My biggest problem(which is kind of small) at the moment is that there aren't any typings to find for this package yet. I would really love to see some typings for this libary as it would make the whole API easier to get into and make it way easier to use in Typescript projects :-)
The text was updated successfully, but these errors were encountered: