Skip to content

Commit ad9129b

Browse files
committed
feat: new type UnionToSubTupleArray & fix LiteralUnion
1 parent a82a5c5 commit ad9129b

File tree

9 files changed

+87
-4
lines changed

9 files changed

+87
-4
lines changed

.lintstagedrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"*.{ts,tsx}": ["npm run test", "npm run lint:eslint", "npm run format"],
2+
"*.{ts,tsx}": ["npm run lint:eslint", "npm run format"],
33
"*.{md,yaml,yml,json,html}": ["npm run format"]
44
}

docs/types-kit.getsubtuple.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [types-kit](./types-kit.md) &gt; [GetSubTuple](./types-kit.getsubtuple.md)
4+
5+
## GetSubTuple type
6+
7+
<b>Signature:</b>
8+
9+
```typescript
10+
type GetSubTuple<
11+
L extends number,
12+
V,
13+
R extends readonly unknown[] = []
14+
> = R['length'] extends L ? [...R, V] : GetSubTuple<L, V, [...R, V]>
15+
```
16+
<b>References:</b> [GetSubTuple](./types-kit.getsubtuple.md)
17+

docs/types-kit.literalunion.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Allows creating a union type by combining primitive types and literal types with
99
<b>Signature:</b>
1010

1111
```typescript
12-
export type LiteralUnion<T extends U, U> = T | (U & {})
12+
export type LiteralUnion<T extends U, U> = T | (U & Omit<U, PropertyKey>)
1313
```
1414
1515
## Example

docs/types-kit.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
| [Flat](./types-kit.flat.md) | Create an array that includes the flatten array type. |
3838
| [FlattedArrayItem](./types-kit.flattedarrayitem.md) | Get the flatted item type from an array. |
3939
| [Get](./types-kit.get.md) | Get the specified value from T. |
40+
| [GetSubTuple](./types-kit.getsubtuple.md) | |
4041
| [If](./types-kit.if.md) | If for types. |
4142
| [IfElseIf](./types-kit.ifelseif.md) | If/Else if for types. |
4243
| [IfExtends](./types-kit.ifextends.md) | If Condition\[0\] extends Condition\[1\], return Case1, else return Case2. |
@@ -117,6 +118,7 @@
117118
| [TupleToObject](./types-kit.tupletoobject.md) | Convert a tuple to an object, it can pass in a tag to modify the key value. |
118119
| [TupleToUnion](./types-kit.tupletounion.md) | Convert a tuple to union type. |
119120
| [UnionToIntersection](./types-kit.uniontointersection.md) | Convert union type to Intersection type. |
121+
| [UnionToSubTupleArray](./types-kit.uniontosubtuplearray.md) | Get all sub tuple array from the passing parameter. |
120122
| [UnionToTuple](./types-kit.uniontotuple.md) | Convert union type to a tuple. |
121123
| [UrlParamsToUnion](./types-kit.urlparamstounion.md) | Parser the params of a url into a union type |
122124
| [UrlQueryToObject](./types-kit.urlquerytoobject.md) | Parser the querystring of a url into an object type |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [types-kit](./types-kit.md) &gt; [UnionToSubTupleArray](./types-kit.uniontosubtuplearray.md)
4+
5+
## UnionToSubTupleArray type
6+
7+
Get all sub tuple array from the passing parameter.
8+
9+
<b>Signature:</b>
10+
11+
```typescript
12+
export type UnionToSubTupleArray<T> = TupleKeys<UnionToTuple<T>> extends infer V
13+
? V extends number
14+
? GetSubTuple<V, T>
15+
: never
16+
: never
17+
```
18+
<b>References:</b> [TupleKeys](./types-kit.tuplekeys.md)<!-- -->, [UnionToTuple](./types-kit.uniontotuple.md)<!-- -->, [GetSubTuple](./types-kit.getsubtuple.md)
19+
20+
## Example
21+
22+
23+
```ts
24+
// Expect: ['a' | 'b'] | ['a' | 'b', 'a' | 'b']
25+
type Foo = UnionToSubTupleArray<'a' | 'b'>
26+
```
27+

src/convert/convert.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { StringToNumber } from './string-to-number'
55
import { TupleToObject } from './tuple-to-object'
66
import { TupleToUnion } from './tuple-to-union'
77
import { UnionToIntersection } from './union-to-intersection'
8+
import { UnionToSubTupleArray } from './union-to-sub-tuple-array'
89
import { UnionToTuple } from './union-to-tuple'
910
import { UrlParamsToUnion, UrlQueryToObject } from './url-to-other'
1011

@@ -58,6 +59,13 @@ type TestUrlParamsToUnion = Expect<
5859

5960
type TestLiteralToPrimitive = Expect<LiteralToPrimitive<'a'>, string>
6061

62+
type TestUnionToSubTupleArray = Expect<
63+
UnionToSubTupleArray<'a' | 'b' | 'c'>,
64+
| ['a' | 'b' | 'c']
65+
| ['a' | 'b' | 'c', 'a' | 'b' | 'c']
66+
| ['a' | 'b' | 'c', 'a' | 'b' | 'c', 'a' | 'b' | 'c']
67+
>
68+
6169
export type Result = Test<
6270
[
6371
TestStringToNumber,
@@ -68,6 +76,7 @@ export type Result = Test<
6876
TestOtherToString,
6977
TestUrlQueryToObject,
7078
TestUrlParamsToUnion,
71-
TestLiteralToPrimitive
79+
TestLiteralToPrimitive,
80+
TestUnionToSubTupleArray
7281
]
7382
>

src/convert/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export * from './string-to-number'
44
export * from './tuple-to-object'
55
export * from './tuple-to-union'
66
export * from './union-to-intersection'
7+
export * from './union-to-sub-tuple-array'
78
export * from './union-to-tuple'
89
export * from './url-to-other'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { TupleKeys } from '../mapper'
2+
import { UnionToTuple } from './union-to-tuple'
3+
4+
type GetSubTuple<
5+
L extends number,
6+
V,
7+
R extends readonly unknown[] = []
8+
> = R['length'] extends L ? [...R, V] : GetSubTuple<L, V, [...R, V]>
9+
10+
/**
11+
* Get all sub tuple array from the passing parameter.
12+
* @example
13+
* ```ts
14+
* // Expect: ['a' | 'b'] | ['a' | 'b', 'a' | 'b']
15+
* type Foo = UnionToSubTupleArray<'a' | 'b'>
16+
* ```
17+
*/
18+
export type UnionToSubTupleArray<T> = TupleKeys<UnionToTuple<T>> extends infer V
19+
? V extends number
20+
? GetSubTuple<V, T>
21+
: never
22+
: never

src/union/literal-union.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
* const foo:Foo = '' // You will get auto-completion for `dog` and `cat` literals.
88
* ```
99
*/
10-
export type LiteralUnion<T extends U, U> = T | (U & {})
10+
// Omit<U, PropertyKey> => {}
11+
export type LiteralUnion<T extends U, U> = T | (U & Omit<U, PropertyKey>)
12+
// /**
13+
// * @deprecated see https://github.com/microsoft/TypeScript/pull/49119
14+
// */
15+
// export type LiteralUnion<T extends U, U> = T | (U & {})

0 commit comments

Comments
 (0)