Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion types/react/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ declare namespace React {
// In the future, if we can define its call signature conditionally
// on the existence of `children` in `P`, then we should remove this.
readonly props: Readonly<{ children?: ReactNode }> & Readonly<P>;
readonly state: null | Readonly<S>;
Copy link
Copy Markdown
Contributor

@Hotell Hotell Jun 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a rollback, you need to remove null from the definition as well

otherwise there will be still errors:

image

this will still give you might be null errors when you're gonna read the state, which is again, idiomatic typescript - properties need to be defined on class if you gonna use them in advance, or to be more precise -> TS is using state definition from base class as it was not overridden in my component

state: null | Readonly<S>;
Copy link
Copy Markdown
Member

@Jessidhia Jessidhia Jun 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am too late in making this comment, but I don't see why removing this readonly is at all a fix. According to TypeScript rules, readonly does not affect whether a property is writable in the constructor.

The readonly should be here to guard against assigning to state outside the constructor, which is invalid according to both TypeScript and React rules.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, ideally, it would be nice to be able to use conditional types to make state abstract if the type of S is not empty; this would probably fix all the problems with state being left accidentally uninitialized without adding this null to the type. The default also should be null, and not {}, but apparently that breaks some of the mapped types that depend on S.

Copy link
Copy Markdown
Author

@magnetnation magnetnation Jun 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are partially correct. Readonly properties are writable in the constructors of the the class that defines the property. Not in subclasses. But it is completely valid and recommended to do so in React. See microsoft/TypeScript#25134 (comment) for more details.

I would like to emphasize that union with null is the correct way to go as react state is null unless initialized. Therefore this typescript construction describes it in the best possible way.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what @magnetnation said.

The readonly should be here to guard against assigning to state outside the constructor, which is invalid according to both TypeScript and React rules.

That wont help, when you'll define state on your class via class property, it will override Base class readonly state and will left you just with state so you need to provide the readonly explicitly ( this might be issue on TS level )

readonly state from base class ( correct errors, but wrong runtime as no state has been defined )
image

readonly overridden:
image

The default also should be null, and not {}

yeah that's right, also props should be object not {} etc, which would introduce another breaking changes :)

/**
* @deprecated
* https://reactjs.org/docs/legacy-context.html
Expand Down