You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The PR furthermore adds the following rules:
The operation keyof { [P in K]: X } is equivalent to just K. For example, keyof Partial<T> is equivalent to keyof T.
... I am afraid that it might only work because of a bug, for the following reasons:
Shouldn't nonNullable1?: number; be the same as nonNullable1: number | undefined;? If so, calling the function like this setState({ nonNullable1: undefined }) should report no error.
@ahejlsberg's comment does not hold here, because if I change the definition to: _patch: {[P in keyof State]: State[P]}, TS complains in all the function calls below in the code extract and the mapped type definition becomes:
Is this behavior here to stay, or it results from a bug in TS, and
If this particular behavior is by design, can this solution be considered a safe setState() definition to use in React with strictNullChecks?
Full code example
interfaceState{nonNullable1: number;nonNullable2: string;nullable?: string;}functionsetState(_patch: {[PinkeyofPartial<State>]: State[P]}): void{}setState({});// passessetState({nullable: "test",});// passessetState({nullable: undefined,});// passessetState({nonNullable1: 1,});// passessetState({nonNullable1: undefined,});// Argument of type '{ nonNullable1: undefined; }' is not assignable to parameter of type '{ nonNullable1?: number; nonNullable2?: string; nullable?: string | undefined; }'.// Types of property 'nonNullable1' are incompatible. Type 'undefined' is not assignable to type 'number'.
The text was updated successfully, but these errors were encountered:
TypeScript Version: 2.1.4
The idea
I came up with this definition of React's
setState
, that should offer (understrictNullChecks
) the following:State
type, even without non-null properties specifiednull | undefined
Potential issues
It seems that this satisfies my requirements, as you can see below in the code extract, but when I look at the mapped type definition:
... and one particular statement in #12351:
... I am afraid that it might only work because of a bug, for the following reasons:
nonNullable1?: number;
be the same asnonNullable1: number | undefined;
? If so, calling the function like thissetState({ nonNullable1: undefined })
should report no error._patch: {[P in keyof State]: State[P]}
, TS complains in all the function calls below in the code extract and the mapped type definition becomes:Questions
setState()
definition to use in React withstrictNullChecks
?Full code example
The text was updated successfully, but these errors were encountered: