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
interfaceState{isReady: false;}// Does not workclassBase1<TextendsState>{myState: Readonly<T>={isReady: false};// NopemyOtherState: T={isReady: false};// NopemySecondState: T=null;// Sure!theThirdEstate: T=undefined// Ok!setState(state: Partial<T>){this.myState=Object.assign(this.myState,state);}update(){this.setState({isReady: false});// Nope}}// Works with 'as T'classBase2<TextendsState>{myState: Readonly<T>={isReady: false}asT;myOtherState: T={isReady: false}asT;setState(state: Partial<T>){this.myState=Object.assign(this.myState,state);}update(){this.setState({isReady: false}asT);}}
Expected behavior:
The constraint of T as having a property isReady is flowed through and { isReady: false } is considered to have the same shape as T and is valid for T, Partial<T> or Readonly<T> without having use the explicit type assertion.
Actual behavior:
Manual type assertion is required but then compiles. Null and undefined are the only valid values to assign these class fields.
I suppose I could also replace all references to T outside of the generic constraint with State but that's no fun.
The text was updated successfully, but these errors were encountered:
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: 2.4.1
Code
Typescript Playground
Expected behavior:
The constraint of T as having a property
isReady
is flowed through and{ isReady: false }
is considered to have the same shape asT
and is valid forT
,Partial<T>
orReadonly<T>
without having use the explicit type assertion.Actual behavior:
Manual type assertion is required but then compiles. Null and undefined are the only valid values to assign these class fields.
I suppose I could also replace all references to
T
outside of the generic constraint withState
but that's no fun.The text was updated successfully, but these errors were encountered: