WIP: [react] more strict typings with breaking changes#26956
WIP: [react] more strict typings with breaking changes#26956Hotell wants to merge 4 commits intoDefinitelyTyped:masterfrom
Conversation
Breaking Changes: - if your component supports children prop you need to explicitly define it in your component definition - state is null by default also marked as readonly, you need to set state via class property or if you're initializing it from constructor explicitly provide typescript prop definition - all strict flags are now enabled - Component,PureComponent are now abstract so you have to implement always render which will mitigate any previous runtime errors on compile time
…ect react definition changes
|
@Hotell Thank you for submitting this PR! 🔔 @johnnyreilly @bbenezech @pzavolinsky @digiguru @ericanderson @morcerf @tkrotoff @DovydasNavickas @onigoetz @theruther4d @guilhermehubner @ferdaber @jrakotoharisoa @MartynasZilinskas - please review this PR in the next few days. Be sure to explicitly select If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead. |
|
@Hotell The Travis CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! |
|
@Hotell The Travis CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! |
|
@Hotell I haven't seen anything from you in a while and this PR currently has problems that prevent it from being merged. The PR will be closed tomorrow if there aren't new commits to fix the issues. |
|
Bad bot 😞 |
|
@Hotell To keep things tidy, we have to close PRs that aren't mergeable but don't have activity from their author. No worries, though - please open a new PR if you'd like to continue with this change. Thank you! |
|
Hmm so no interest in making things better I see. I guess community/Microsoft is too afraid of pushing things forward/getting better type-safety with React apps. Overall I'm little sad about this. but yeah it's open source === no guarantees :) Just to mention for anyone who will read this, breaking changes will be necessary sooner than later to make |
It would have been wise to ping the authors of the library typings which started failing due to this breaking change and coordinate with them, instead of just letting the failed CI build sit there until the bot closes the PR. You can't expect all the authors to regularly check for PRs to the main react package. |
|
It pinged the react typing maintainers. Not the maintainers of the typings of the other dozen or so libraries you broke with this change. You may want to check the CI log and see what your change broke in other typings... |
|
@Hotell interface Props {
a(b: string, c: string | number): number; // see c argument
b: {
c(b: string, c: string | number): number;
}
}
type StrictifyProps<T> = {
[K in keyof T]:
T[K] extends (...args: infer A) => infer R
? (...a: A) => R // making property from method
: T[K] extends Object //go deeply
? StrictifyProps<T[K]>
: T[K]
};
const a = (b: string, c: string) => 42; // see c argument
type P = StrictifyProps<Props>;
let x!: P;
// x.
let c: P["a"] = a; //now has error
let b: P['b'] = {
c: a //now has error
}The code above fixes this problem. All is needed is to add this to react typings, where we have |

This reintroduces changes from #26813 and adds other type safety improvements
Breaking Changes:
Before:
After:
Readonly<S> | nullunion type. In runtime if state is not set it'snullby default. You need to set state via class property or if you're initializing it from constructor explicitly provide typescript property definition, to tell the compiler that you're gonna use state which will be defined within constructor instead of property.Update:
I reverted marking state as readonly -> based on this discussion
Before:
After:
all TS strict flags are now enabled 🖖
Component,PureComponent are now abstract so you have to implement always render which will mitigate any previous runtime errors on compile time
Before:
After:
type CElement<T extends Component, P = T['props']> = ComponentElement<T, P>;interface ComponentElement<T extends Component, P = T['props']> extends ReactElement<P> {objectwhich describes runtime ( if you won't provide props, it will be empty object by default ){}is likemixedin Flow ( which allows to set primitives and non-primitive values as wellBefore:
After:
npm test.)npm run lint package-name(ortscif notslint.jsonis present).If changing an existing definition:
tslint.jsoncontaining{ "extends": "dtslint/dt.json" }.