在返回函数中'{}'的含义是什么,该打字稿语法如何解释为ES7。
export const genericRetryStrategy = ({
maxRetryAttempts = 3,
scalingDuration = 1000,
excludedStatusCodes = []
}: {
maxRetryAttempts?: number,
scalingDuration?: number,
excludedStatusCodes?: number[]
} = {}) => (attempts: Observable<any>) => {
//FUNCTION IMPLEMENTATION
};
让我们将其分解。您的功能声明基本上具有此结构:
export const myFunc = (<parameter>: <type> = <default>) => <implementation>;
声明的<parameter>
部分是一种去结构的模式,可提取3个特定属性并将这些属性值传递给您的函数主体。每个属性都是可选的,并且在情况下为 undefined
,在情况下给出一个默认值。
<type>
部分只是声明了预期的参数类型,在这种情况下,一个包含三个属性的对象,所有这些都可以是可选的。
<default>
部分指示如果参数为 undefined
或不提供任何参数,则该值将替换为此参数。这使您完全没有参数调用您的功能,即以下所有内容都是等效的:
genericRetryStrategy({ maxRetryAttempts: 3, scalingDuration: 1000, excludedStatusCodes: [] });
genericRetryStrategy({}); // use default property values
genericRetryStrategy(); // use default parameter value
希望能清除语法。要将其转换为ES7,您要做的就是摆脱声明的<type>
部分,如:
export const genericRetryStrategy = ({
maxRetryAttempts = 3,
scalingDuration = 1000,
excludedStatusCodes = []
} = {}) => (attempts) => {
//FUNCTION IMPLEMENTATION
};