The given code is functional, but I am looking to refactor it for better clarity using Typescript syntax if possible.
class Actions { actions: string[] }
type Argument = object | Actions;
export class GetFilesContext implements IExecutable {
execute( args: Argument, response: ServerResponse<IncomingMessage> ): void {
...
// eslint-disable-next-line no-prototype-builtins
if( args && args.hasOwnProperty( "actions" )) {
ctxt.actions.length = 0;
ctxt.actions.push( ...(args as Actions).actions );
}
response.end( JSON.stringify( ctxt ));
}
}
The conditional check:
// eslint-disable-next-line no-prototype-builtins
if( args && args.hasOwnProperty( "actions" )) {
translates to "if args is defined and contains an attribute called actions"
How can this be rewritten?
I attempted to use typeof
and instanceof
, but without success.