-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Fix #466: Type of setState argument should be Partial<StateType> #467
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
Conversation
@@ -52,7 +52,7 @@ declare namespace preact { | |||
|
|||
linkState:(name:string) => (event: Event) => void; | |||
|
|||
setState(state:StateType, opts?:any):void; | |||
setState(state:Partial<StateType>, opts?:any):void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is cool. I've been making most of my State prop declarations optional, which is exactly what that generic type does https://github.com/Microsoft/TypeScript/pull/12276/files#diff-a6b488d9bd802977827b535a3011c1f3R1350
My only issue with using this is that it requires typescript@next
if I'm not mistaken, and this could break some folks projects if released. I'm personally using typescript@next
, so I'm good :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[email protected] supports this, but yeah, it's a bit bleeding edge. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome. Using Partial is the way to go here. Just wish there was a way for libraries to specify what version of TypeScript their definition files require to compile.
Changes Unknown when pulling c08c7e2 on mseddon:setstate-typing into ** on developit:master**. |
I've just done some testing with TypeScript 2.0 and it looks like it is quite easy for users of earlier versions of TypeScript to add a "polyfill" for type Partial<T> = T; With this, any reference to // Note: Partial<T> is a TS 2.1 feature. See <link to this PR>
setState(state: Partial<State>) Thoughts? |
@robertknight The problem with that "polyfill" is that it's not. Nothing would be optional. Instead aliasing as any would be closer even :( I think saying that you need TS 2.1+ is fine. TS 2.0 to 2.1 does not introduce breaking changes, even we upgraded from 1.3 to 2.1 and it was fine. Everything was stashed away with flags anyway. |
That's why I put "polyfill" in quotes. It would then work the same as it does now using the current So to update the status here, the current recommendation from the TypeScript team based on the discussion for the React typings is that there should be a grace period after a new TS release before releasing typings updates that depend on the new release. I would suggest that we track DefinitelyTyped/DefinitelyTyped#13155 and once that is merged then we can do the same here. That way Preact and React typings will be in sync. |
Then yup. I completely agree.
…On Fri, 13 Jan 2017, 20:15 Robert Knight, ***@***.***> wrote:
@robertknight <https://github.com/robertknight> The problem with that
"polyfill" is that it's not.
Nothing would be optional. Instead aliasing as any would be closer even :(
That's why I put "polyfill" in quotes. It would then work the same as it
does now using the current setState typings, so there would be no
regression. Once users update to TS 2.1 they will be able to take advantage
of the proper feature.
So to update the status here, the current recommendation from the
TypeScript team based on the discussion for the React typings is that there
should be a grace period after a new TS release before releasing typings
updates that depend on the new release.
I would suggest that we track DefinitelyTyped/DefinitelyTyped#13155
<DefinitelyTyped/DefinitelyTyped#13155> and once
that is merged then we can do the same here. That way Preact and React
typings will be in sync.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#467 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AB35WDbkQONjrDf7rGAmk8nQEvTp_Dntks5rR807gaJpZM4LS_xP>
.
|
The corresponding PR for React has been merged upstream. |
@robertknight yes. The dropped "optional key" support in state and chose pick. DefinitelyTyped/DefinitelyTyped#13155 based of microsoft/TypeScript#12793 (comment) Preact could follow this. |
@@ -45,7 +45,7 @@ declare namespace preact { | |||
|
|||
linkState:(name:string) => (event: Event) => void; | |||
|
|||
setState(state:StateType, callback?:() => void):void; | |||
setState(state:Partial<StateType>, callback?:() => void):void; | |||
setState(fn:(prevState:StateType, props:PropsType) => StateType, callback?:() => void):void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be able to return a partial state here as well.
d9a0755
to
37c066f
Compare
37c066f
to
e063e2b
Compare
Updated to use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just given this a quick test with TypeScript 2.1.5. Works nicely. Thanks @mseddon !
🎉 |
Awesome, so happy to have this merged :D |
No description provided.