user21
user21

Reputation: 1351

Simplify the JavaScript code using TypeScript

I am wondering if there is way to simplify the following code:

this._session = AmberFile.session;
this._sessionPath = AmberFile.sessionPath;
this._sessionDevice = AmberFile.sessionDevice;
this._ssconfig = AmberFile.ssconfig;

My attempt using TypeScript/ES6, but it doesn't look like it has cleaner code:

const {session, sessionPath, sessionDevice, ssconfig) = AmberFile
this._session = session;
this._sessionPath = sessionPath;
this._sessionDevice = session;
this._ssconfig = sessionPath;

let context: any = {  
  sessionPath: this._sessionPath, 
  session: this._session,
  sessionDevice: this._sessionDevice
}

Upvotes: 1

Views: 98

Answers (2)

GreenTeaCake
GreenTeaCake

Reputation: 858

const {
    session: _session,
    sessionPath: _sessionPath,
    sessionDevice: _sessionDevice
} = AmberFile;
Object.assign(this, { _session, _sessionPath, _sessionDevice });

See TS Playground Example

Upvotes: 2

Aminadav Glickshtein
Aminadav Glickshtein

Reputation: 24650

Yes. There is:

 Object.assign(this,{
         _session:AmberFile.session,
         _sessionPath:AmberFile.sessionPath
 })

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

Read more:

Upvotes: 2

Related Questions