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
class T {
static map = new Map<string, object>();
static getInstanceById(id:string): object {
if ( !T.map.has( id ) ) {
T.map.set( id, {} );
}
return T.map.get( id ); // >>>
}
}
/** <<<
* TS2322: Type 'object | undefined' is not assignable to type 'object'.
* Type 'undefined' is not assignable to type 'object'.
*/
// very bad!
class T0 {
static map = new Map<string, object>();
static getInstanceById(id:string): object {
let result = T0.map.get( id );
if ( !result ) {
result = {};
T0.map.set( id, result );
}
return result;
}
}
// Good!
class T1 {
static map = new Map<string, object>();
static getInstanceById(id:string): object {
return T1.map.has( id ) ? T1.map.get( id ) : T1.map.set( id, {} ).get( id );
}
}
The text was updated successfully, but these errors were encountered:
Uh oh!
There was an error while loading. Please reload this page.
The text was updated successfully, but these errors were encountered: