ALopez
ALopez

Reputation: 131

Typescript return empty object type syntax

What is the meaning of '{}' in the return function, how that typescript syntax could be interpreted to es7.

export const genericRetryStrategy = ({
  maxRetryAttempts = 3,
  scalingDuration = 1000,
  excludedStatusCodes = []
}: {
  maxRetryAttempts?: number,
  scalingDuration?: number,
  excludedStatusCodes?: number[]
} = {}) => (attempts: Observable<any>) => {
    //FUNCTION IMPLEMENTATION
};

Upvotes: 0

Views: 1156

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149050

Let's break it down. Your function declaration basically has this structure:

export const myFunc = (<parameter>: <type> = <default>) => <implementation>;

The <parameter> portion of the declaration is a de-structured pattern which extracts the 3 specific properties and passes those property values to your function body. Each property is optional and is given a default value in the case it's value is undefined in the argument.

The <type> portion simply declares what type of argument is expected, in this case an object that contains three properties, all of which may be optional.

The <default> portion indicates what value will be substituted for this parameter if the argument is undefined or if none is provided. This allows you to call your function with no arguments at all, i.e. all of the following are equivalent:

genericRetryStrategy({ maxRetryAttempts: 3, scalingDuration: 1000, excludedStatusCodes: [] });
genericRetryStrategy({});  // use default property values
genericRetryStrategy();    // use default parameter value

Hopefully that clears up the syntax. To convert this to es7, all you have to do is get rid of the <type> portion of the declaration, like so:

export const genericRetryStrategy = ({
  maxRetryAttempts = 3,
  scalingDuration = 1000,
  excludedStatusCodes = []
} = {}) => (attempts) => {
    //FUNCTION IMPLEMENTATION
};

Upvotes: 2

Related Questions