You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript定义Redux action函数接口报number重复标识符错误如何解决?

问题原因

你在定义fetchRegisterAll的函数入参类型时,混淆了TypeScript对象类型声明和JavaScript对象解构重命名的语法:
你写的({ limit: number, skip: number })会被TypeScript识别为「对入参对象做解构,将limit字段重命名为number变量、skip字段也重命名为number变量」,直接触发两个报错:

  • 两个变量都使用number作为名称,触发Duplicate identifier 'number'重复标识符错误
  • 重命名后的number变量没有显式定义类型,触发Binding element 'number' implicitly has an 'any' type隐式any类型错误
解决方案

有两种常用的修正方式,按需选择即可:

方式1:直接内联定义入参类型

直接在参数位置声明整个入参对象的类型,修改后的接口代码如下:

interface TabNavUnitsProps {
  unitsToRegister: number,
  unit: { status: string, unitsRegisterCount: number },
  // 注意:类型标注写在整个参数对象的后面
  fetchRegisterAll: (params: { limit: number, skip: number }) => Promise<void>
}

如果后续你在函数实现时需要直接解构拿到limit和skip字段,写法如下:

const fetchRegisterAll = ({ limit, skip }: { limit: number; skip: number }) => {
  // 你的业务逻辑
}

方式2:抽离复用入参类型

如果这个入参结构在多个地方用到,可以单独抽成接口提高复用性:

// 单独抽离入参类型,可全局复用
interface FetchRegisterAllParams {
  limit: number
  skip: number
}

interface TabNavUnitsProps {
  unitsToRegister: number,
  unit: { status: string, unitsRegisterCount: number },
  fetchRegisterAll: (params: FetchRegisterAllParams) => Promise<void>
}

内容的提问来源于stack exchange,提问作者xyz83242

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 11:24:08