typescript - 打字稿返回空对象类型语法
问题描述
返回函数中'{}'的含义是什么,该打字稿语法如何解释为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
};
推荐阅读
- javascript - cant connect to dockerized postgresql server
- scala - 来自类型参数的 WeakTypeOf
- html - 导航栏的右侧部分不显示过渡
- laravel - 如何使用 where 子句在 laravel 中编写查询?
- git - 如何更改已提交的多个文件名称的大小写?
- python - 使用 Python 查找字符的位置以及在文本中使用的次数
- python - 如何在此代码中理解卷积神经网络的输入图像
- python - OSM 在 Python 中生成具有空间约束的路线
- python - python中的约束线性优化问题
- javascript - 在客户端使用 NodeJs 播放音频文件