typescript 如何忽略泛型参数啊?

5 天前
 hahaFck
比如我定义了一个类型带有泛型参数<T>

```typescript
export interface ReportTreeNode<T extends BaseDataType> {
data: T
children: ReportTreeNode<T>[]
relationshipWithParent: RelationshipWithParent
nodeType: NodeType
cellInfo?: CellInfo
}
```

但是我在写一些其他工具方法时,是不关心具体类型 T 的,在 java 中这个种情况可以写问号`?`代替,但是 typescript 不行,如果我写 any 的话,eslint 又提示我应该用 never ,我改成 never 后,调用这个方法时又报`<never>不能赋值给<T>`
260 次点击
所在节点    TypeScript
6 条回复
Opportunity
5 天前
给工具方法再加个类型参数,然后继续写 T 呗
xiaoming1992
5 天前
添加一个默认参数,如 BaseDataType

```typescript

export interface ReportTreeNode<T extends BaseDataType = BaseDataType> {

}

```
xiaoming1992
5 天前
或者用 any ,再忽略 eslint 提示
hahaFck
5 天前
@xiaoming1992 #2 嗯,ts 好像只能在有默认参数的时候才能忽略掉泛型
hahaFck
5 天前
@Opportunity 写多了,感觉好麻烦,关键有的时候方法就不关心泛型呢
yanyiming
4 天前
那就把默认参数设为 any.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1052423

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX