1
noe132 2022-03-07 17:19:05 +08:00
你这已经是最简洁的写法了
|
2
f360family123 OP @noe132 可是继续加 SearchType 不就很难受了
|
3
anjianshi 2022-03-07 17:24:54 +08:00
确实是最简洁的写法了...
|
4
anjianshi 2022-03-07 17:26:28 +08:00
如果新加的 type 不需要 xSearchItems ,可以这样合并:
```typescript type Column = { xSearchType: 'input' | 'other' | 'other-other' } | { xSearchType: 'select', xSearchItems: any[] } ``` |
5
zhy0216 2022-03-07 17:29:28 +08:00
应该可以用 conditional type
|
6
xiaojie668329 2022-03-07 20:15:16 +08:00
```typescript
type column<T> = T extends 'select' ? { xSearchType: T, xSearchItems: any[] } : { xSearchType: T } let c: column<'select'> = { xSearchType: 'select', xSearchItems: [], } let n: column<'input'> = { xSearchType: 'input', } ``` 暂时想到这样写,不过感觉还有更简洁的方法。 |
7
zhihaofans 2022-03-07 20:29:41 +08:00 via iPhone
没学过 ts 的好奇问一下,参数名前面都带个 x 是有什么用途
|
8
f360family123 OP @zhy0216 看了下文档,没想出来怎么实现
|
9
f360family123 OP @zhihaofans 我随手写的..,跟 ts 没关系
|
10
f360family123 OP @xiaojie668329 要传泛型感觉也有点难受
|
11
zhy0216 2022-03-07 20:54:52 +08:00
|