调用带有“选项对象”和多次调用之间共享参数的函数。

huangapple go评论68阅读模式
英文:

Function call with "options object" and shared parameters between multiple calls

问题

I understand that you want a translation of the code without the code part. Here's the translated content you provided:

I am calling a function using an "options object" but I would like to be more DRY. Currently, using a contrived example, I do this:

What would be nicer is if I could, hypothetically, do:

However, the TypeScript compiler says:

TS2345: Argument of type '{ file1: string; params: { file2: string; file3: string; }; file4: string; }' is not assignable to parameter of type '{ file1: string; file2: string; file3: string; file4: string; }'. Object literal may only specify known properties, and 'params' does not exist in type '{ file1: string; file2: string; file3: string; file4: string; }'.

I get that this is not, technically, the same type, but it looks like it should "fit" in there. Is there a way to "explode" the commonParams map so it can be inserted into the arguments? Thereby avoiding duplication.

英文:

I am calling a function using an "options object" but I would like to be more DRY. Currently, using a contrived example, I do this:

// "options" being the options object
function refineMacroData(options: {file1: string, file2: string, file3: string, file4: string}) {
  // Do the stuff
}

// file2 and file3 arguments are the same in both branches for this context
if (isThisTrue()) {
  refineMacroData({
    file1: 'if this is true',
    file2: 'usually the same',
    file3: 'also usually the same',
    file4: 'also if this is true'
  })
} else {
  refineMacroData({
    file1: 'if this is false',
    file2: 'usually the same',
    file3: 'also usually the same',
    file4: 'also if this is false'
  })
}

What would be nicer is if I could, hypothetically, do

// Same function as before
function refineMacroData(options: {file1: string, file2: string, file3: string, file4: string}) {
  // Do the stuff
}

const commonParams=  {file2: 'usually the same', file3: 'also usually the same'}

if (isThisTrue()) {
  refineMacroData({file1: 'something', commonParams, file4: 'another one again'})
} else {
  refineMacroData({file1: 'different', commonParams, file4: 'different again'})
}

However, the Typscript compiler says

> TS2345: Argument of type '{ file1: string; params: { file2: string; file3: string; }; file4: string; }' is not assignable to parameter of type '{ file1: string; file2: string; file3: string; file4: string; }'.   Object literal may only specify known properties, and 'params' does not exist in type '{ file1: string; file2: string; file3: string; file4: string; }'.

I get that this is not, technically, the same type but it looks like it should "fit" in there. Is there a way to "explode" the commonParams map so it can be inserted into the arguments? Thereby avoiding duplication.

答案1

得分: 1

是的,你所称之为“爆破”在JavaScript(和TypeScript)中被称为扩展,可以通过扩展语法(...来实现,如下所示:

if (isThisTrue()) {
  refineMacroData({ file1: 'something', ...commonParams, file4: 'another one again' })
} else {
  refineMacroData({ file1: 'different', ...commonParams, file4: 'different again' })
}

代码示例链接

英文:

Yes, what you're calling "exploding" is known in JavaScript (and TypeScript) as spreading, which can be achieved via spread syntax (...), as follows:

if (isThisTrue()) {
  refineMacroData({ file1: 'something', ...commonParams, file4: 'another one again' })
} else {
  refineMacroData({ file1: 'different', ...commonParams, file4: 'different again' })
}

Playground link to code

huangapple
  • 本文由 发表于 2023年6月13日 11:08:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定