-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Typings for recompose #231
Comments
@R00GER I was just thinking the same thing. In order for this library to be of any use to me I need some high quality typings for it. How much have you done on this? Are you interested in working together to get these finished? |
Hello @ianks. Thanks for the attention |
@R00GER, the typings looks really good. do you plan on contributign them to definitely typed? |
@ianks, yes, I'm interested to proceed with the typings and contribute them to typings' registry. I wanted to hear some feedback but so far I've got the only response from you. |
@R00GER Thank you so much for these. We have been trying to type HoCs for a couple of days but being new to TS has proved it challenging. This helped us a lot as we are using recompose throughout a large project. I will make sure to provide feedback as it comes. |
Thanks for providing a starting point! I just got into TypeScript a couple days ago and recompose was one of the first libraries that I ran into trouble with due to no typings. I decided to write a full definition out with specs for DefinitelyTyped, as a way to get more familiar with TS. Would appreciate your guys feedback/whatever contributions you may have before I submit the PR. |
@ianks That would be ideal! I had started for fun before finding that @R00GER had already wrote this, but some ideas were taken from him (like I am also not sure if everything is implemented 100% correct. I am very very new to TS, but I tried to avoid However, @R00GER, I noticed your repository has much more detailed specs, testing prop intersection/union types etc. Do you think this is necessary for iteration 1? And some of the definitions were crossed out in the readme, but I did not see them in the remote repository. |
I would say that process of making types stricter is not acceptable. Once you will release next version with stricter typings/better inferring you will see type mismatches. I think everyone can admit that it's terrible feeling when your app is broken because of vendors. That's why I stopped at certain point and asked community for feedback. I think that typings are to be good (tested, infers as much as possible) from the very fist version |
Hi So I decided to develop an almost from scratch version of typings, based initially on @R00GER ‘s. After trying with a smaller package (change-emitter from @acdlite as well) I finally got recompose to DefinitellyTyped. It would be nice that all of you take a look at it and help me reach an excelent typings of this great package for the community to use it. |
function getContext<TContext, TProps>(contextTypes: ValidationMap<TContext>): ComponentEnhancer<TContext & TProps, TProps> https://github.com/acdlite/recompose/blob/master/src/packages/recompose/getContext.js#L6 |
@iskandersierra thanks for creating them! |
RxJS is deciding its next API - likely based around a compose/pipe structure - which has triggered a lot of movement on Typescript to finally support this style without explicit types on every argument. This should be extremely helpful in typing recompose. Latest issue: The last month of design notes was almost completely dedicated to this issue: Some people have gotten extremely close to getting literal type subtraction without any syntax changes: This feels like it's getting really close. |
May be https://github.com/acdlite/recompose/tree/master/types can help to create typings for typescript |
I used recompose in a couple of project - in the current one we wanna migrate to typescript. We came up with the following:
This works as expected and outputs:
But the following surprise me by not raising any error:
I would expect the typescript compiler to fail because the "age" property is not provided anymore to the App component. But it compiles just fine. Is this supposed to work or am I doing something completly wrong? Versions: |
I think it’s working properly. To get the behavior you’re looking for, use the second template parameter of compose- e.g. compose<{}, {name:...}>(...)
…-ARO
On Sep 28, 2017, at 1:32 PM, Jan Zimmek ***@***.***> wrote:
I used recompose in a couple of project - in the current one we wanna migrate to typescript.
We came up with the following:
const App = (props: { name: string; age: number }) => {
return (
<div>
name: {props.name}, age: {props.age}
</div>
);
};
const MyApp = compose<{ name: string; age: number }, {}>(
withProps<{ name: string }, {}>(() => ({ name: "joe" })),
withProps<{ age: number }, {}>(props => ({ age: 99 }))
)(App);
const x = renderToString(<MyApp />);
console.info("===>", x);
This works as expected and outputs:
===> <div data-reactroot="">name: <!-- -->joe<!-- -->, age: <!-- -->99</div>
But the following surprise me by not raising any error:
...
const MyApp = compose<{ name: string; age: number }, {}>(
withProps<{ name: string }, {}>(() => ({ name: "joe" })),
// withProps<{ age: number }, {}>(props => ({ age: 99 }))
)(App);
const x = renderToString(<MyApp />);
I would expect the typescript compiler to fail because the "age" property is not provided anymore to the App component. But it compiles just fine.
Is this supposed to work or am I doing something completly wrong?
Versions:
@ types/react: "^16.0.7",
@ types/react-dom: "^15.5.5",
@ types/recompose: "^0.24.2",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"recompose": "^0.25.1"
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@arolson101 thanks for your answer. unfortunately it does not work either, nor any other TInner/TOutter variation. Switching TInner/TOutter fails with: Setting TInner/TOutter also fails: Setting TInner/TOutter compiles if I explicitly set name/age on but is obviously not what I want to do. Almost everything else in typescript/recompose works as expected immediately. Being able to see "missing props of a react component" was one of the reason we even considered to migrate to typescript - loosing this feature would be a shame. But I have to admit that I am relatively new to typescript, so I still hope that I am just doing something wrong here. Any help on this is really appreciated. Please let me know if I should create separate issue. |
Change your App definition to const App = (props: { name: string; age: number } & React.Props<any>) => {
return (
<div>
name: {props.name}, age: {props.age}
</div>
)
} |
export interface WithProps {
name: string
age: number
}
const App = (props: Partial<WithProps>) => {
return <div>{props.name}</div>
}
withProps(() => ({name: 'joe'}))(App) |
I think this is relevant to the conversation as it surveys how far we can get with HOCs in TS + React: https://medium.com/@jrwebdev/react-higher-order-component-patterns-in-typescript-42278f7590fb |
I'am a big fan of recompose but I think the export const withImagesData = (loading?: React.SFC) =>
compose<WithImagesData, {}>(
graphql<{}, ImagesQuery, ImagesQueryArgs>(GetImages),
...(loading
? [
branch<ImageQueryProps>(
props => props.data.loading,
renderComponent(loading)
)
]
: []
),
withProps<WithImagesData, ImageQueryProps>(props => ({
images: props.data.images || []
}))
) All HOCs need to be typed explicitly - this is a huge burden and margin for error because I have to make sure that types match. Would it be possible to infer input/output typings from HOCs so that typings for HOCs can be omitted (as much as possible) and we just set the expected inner and outer type on |
RxJS seems to have come up with a good solution, at least for
There are open tickets in Typescript for variadic types (to avoid the overloads) and better generic inference. It's a hard thing to solve. |
FWIW the compose in redux has pretty good typing |
Looks like they're doing about the same thing: I think I'm wrong about the complexity here. Most HOCs are props => props, and all the extra arguments are in the outer functions. Later today I'll try out |
TypeScript 3.0 should help with some of its new features. For example variadic parameter typing (as opposed to just having static overloaded typing before). |
Thanks for the explanation! I will subscribe to the issue and hope that it get's fixed ;-) |
@threehams Any update on this? Have you tried it with |
Tried and failed, sorry. The good news is that React hooks are dead simple to type. |
@threehams You are right. I guess once the api of hooks will be stable, recompose will become obsolete. |
Is there any movements regarding typescript annotations?
I've started to annotate methods that I need considering
react-redux
's HOC factory (connect()
)E.g.
withContext()
And having such components
We can use it as follows
In other methods (like
getContext
) this solution doesn't work that good so I have to be more verbose on typedefs, for example having annotationTSC can't infer types correctly and thus (
Cmp
is a regularReact.ComponentClass<CmpProps>
)won't blame you so I have to specify types explicitly
So I'm wondering if someone have already started (or finished) to do the same. Or maybe someone knows better way to annotate HOCs and HOC factories.
The text was updated successfully, but these errors were encountered: