‘Typescript: ‘gridTemplateColumns’ 被多次指定,因此此使用将被覆盖。ts(2783)’

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

Typescript: 'gridTemplateColumns' is specified more than once, so this usage will be overwritten.ts(2783)

问题

I upgraded my typescript version from 4.0.5 to 4.9.5 in my react project.

In the following code (styled components):

export const __offerBlock: IStyles = (props: any) => ({
  gridTemplateColumns: "18% auto",
  ...(props.isMobile
    ? {
        margin: "0 15px",
        gridTemplateColumns: "10% auto"
      }
    : {
        width: "98%"
      })
});

I'm getting this error:

'gridTemplateColumns' is specified more than once, so this usage will be overwritten.ts(2783)
__fileNameStyles.ts(158, 3): This spread always overwrites this property.

‘Typescript: ‘gridTemplateColumns’ 被多次指定,因此此使用将被覆盖。ts(2783)’

How do I allow typescript to allow overwrite using spread?
Also, why is the error coming when I'm conditionally adding the "gridTemplateColumns" property.

This error only started after bumping up the ts version.

英文:

I upgraded my typescript version from 4.0.5 to 4.9.5 in my react project.

In the following code (styled components):

export const __offerBlock: IStyles = (props: any) => ({
  gridTemplateColumns: "18% auto",
  ...(props.isMobile
    ? {
        margin: "0 15px",
        gridTemplateColumns: "10% auto"
      }
    : {
        width: "98%"
      })
});

I'm getting this error:

'gridTemplateColumns' is specified more than once, so this usage will be overwritten.ts(2783)
__fileNameStyles.ts(158, 3): This spread always overwrites this property.

‘Typescript: ‘gridTemplateColumns’ 被多次指定,因此此使用将被覆盖。ts(2783)’

How do I allow typescript to allow overwrite using spread?
Also, why is the error coming when I'm conditionally adding the "gridTemplateColumns" property.

This error only started after bumping up the ts version.

答案1

得分: 1

你可以将外部的 gridTemplateColumns 移动到三元运算符的 else 子句中:

export const __offerBlock: IStyles = (props: any) => ({
  ...(props.isMobile
    ? {
        margin: "0 15px",
        gridTemplateColumns: "10% auto"
      }
    : {
        width: "98%",
        gridTemplateColumns: "18% auto"
      })
});

不过,在此之际,我能否建议你进一步修改这段代码?实际上,你正在返回两个不同的字面对象之一,所以你可以像这样编写你的函数:

export const __offerBlock: IStyles = (props: any) => {
  if (props.isMobile) {
    return {
      margin: "0 15px",
      gridTemplateColumns: "10% auto"
    }
  } else {
    return {
      width: "98%",
      gridTemplateColumns: "18% auto"
    }
  }
}
英文:

You can just move the outer gridTemplateColumns into the else clause of the ternary:

export const __offerBlock: IStyles = (props: any) => ({
  ...(props.isMobile
    ? {
        margin: "0 15px",
        gridTemplateColumns: "10% auto"
      }
    : {
        width: "98%",
        gridTemplateColumns: "18% auto"
      })
});

However, while I'm at it, might I suggest that you change this even further? What you are basically doing here is returning one of two different literal objects, so you can just write your function like that instead:

export const __offerBlock: IStyles = (props: any) => {
  if (props.isMobile) {
    return {
      margin: "0 15px",
      gridTemplateColumns: "10% auto"
    }
  } else {
    return {
      width: "98%",
      gridTemplateColumns: "18% auto"
    }
  }
}

</details>



huangapple
  • 本文由 发表于 2023年3月9日 21:07:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685045.html
匿名

发表评论

匿名网友

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

确定