Skip to content

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

Merged
merged 2 commits into from
Jan 29, 2017

Conversation

mseddon
Copy link
Contributor

@mseddon mseddon commented Dec 21, 2016

No description provided.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 97.513% when pulling 386bb3a on mseddon:setstate-typing into 75acb15 on developit:master.

@developit
Copy link
Member

/cc @kruczy @jrf0110 who know a bunch about tsd

@coveralls
Copy link

Coverage Status

Coverage remained the same at 97.522% when pulling 9fbcdb1 on mseddon:setstate-typing into 074aa7a on developit:master.

@@ -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;
Copy link
Contributor

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 :)

Copy link
Contributor Author

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. :)

Copy link
Contributor

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.

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling c08c7e2 on mseddon:setstate-typing into ** on developit:master**.

@robertknight
Copy link
Member

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 Partial<T> which would allow them to use these latest definitions:

type Partial<T> = T;

With this, any reference to Partial<T> is treated as if it were just T. On that basis, one option is that we could make this change but perhaps add a comment in the type definitions for the benefit of anyone using earlier TS versions, eg:

// Note: Partial<T> is a TS 2.1 feature. See <link to this PR>
setState(state: Partial<State>)

Thoughts?

@SleeplessByte
Copy link

@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.

@robertknight
Copy link
Member

@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 and once that is merged then we can do the same here. That way Preact and React typings will be in sync.

@SleeplessByte
Copy link

SleeplessByte commented Jan 13, 2017 via email

@robertknight
Copy link
Member

The corresponding PR for React has been merged upstream. setState in the React definitions uses Pick rather than Partial: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/99d41f818c2de87790ce1f02956cbc39bce40f40/react/index.d.ts#L169

@coveralls
Copy link

Coverage Status

Coverage remained the same at 97.695% when pulling d9a0755 on mseddon:setstate-typing into 472cb2b on developit:master.

@SleeplessByte
Copy link

@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;
Copy link
Contributor

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.

@mseddon
Copy link
Contributor Author

mseddon commented Jan 25, 2017

Updated to use Pick, and added the other signature.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 97.695% when pulling e063e2b on mseddon:setstate-typing into 472cb2b on developit:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 97.695% when pulling a7d85f4 on mseddon:setstate-typing into 76fcbee on developit:master.

Copy link
Member

@robertknight robertknight left a 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 !

@robertknight robertknight merged commit 8614396 into preactjs:master Jan 29, 2017
@mseddon
Copy link
Contributor Author

mseddon commented Jan 30, 2017

🎉

@developit
Copy link
Member

Awesome, so happy to have this merged :D

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.

7 participants