typescript - 如何在打字稿中将参数从一个具有可变参数数量的函数传递给另一个具有可变参数数量的函数?
问题描述
这不起作用
static myDebug(message: string, ...optionalParameters: any[]): void {
// tslint:disable-next-line
console.debug(message, optionalParameters);
}
它只是写这样的行
test myDebug this.url = Array(2), windowId = %d
代替
test myDebug this.url = /myurl, windowId = 123
在 C 中这是无法做到的,所以我需要这样做:
va_start(Args, Message);
vsnprintf_s(Buffer, sizeof(Buffer), _TRUNCATE, Format, Args);
va_end(Args);
CallTheFunction(Buffer);
解决方案
如果要记录剩余数组的条目而不是剩余数组本身,请将其展开:
static myDebug(message: string, ...optionalParameters: any[]): void {
// tslint:disable-next-line
console.debug(message, ...optionalParameters);
// --------------------^^^
}
扩展语法在许多方面是对其余语法的补充。
使用它,如果你myDebug
这样调用:
myDebug("hi there", 1, 2, 3);
它会这样调用console.debug
(有效地):
console.debug(message, optionalParameters[0], optionalParameters[1], optionalParameters[2]);
推荐阅读
- python - 相控解码器的 Scipy 替换
- python - asyncpg 中的 fetchval 是否适用于 sql 注入?
- r - 文本作为 R 中管道函数的输出
- selenium - Selenium - 在多个 TestNG @Test-annotations 中使用文本字符串
- java - 在 Java 中实现 SAT 多边形碰撞检测时遇到问题
- css - DIV(位置:固定)内的 DIV(溢出-y:滚动)溢出父级边界
- mpi - 加载共享库时 MPI 错误:libpgftnrtl.so 在另一台机器上
- vba - 如何等到从 UFT 的网页中删除/删除对象?
- pandas - 计算出的 pandas 列的最大值和 0
- npm - Vue CLI 3 - 将 TailwindCss 1.9 升级到 TailwindCss 2.0 时出错