File size: 2,245 Bytes
c2b7eb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { Tuple } from '@reduxjs/toolkit'

describe('type tests', () => {
  test('compatibility is checked between described types', () => {
    const stringTuple = new Tuple('')

    expectTypeOf(stringTuple).toEqualTypeOf<Tuple<[string]>>()

    expectTypeOf(stringTuple).toMatchTypeOf<Tuple<string[]>>()

    expectTypeOf(stringTuple).not.toMatchTypeOf<Tuple<[string, string]>>()

    const numberTuple = new Tuple(0, 1)

    expectTypeOf(numberTuple).not.toMatchTypeOf<Tuple<string[]>>()
  })

  test('concat is inferred properly', () => {
    const singleString = new Tuple('')

    expectTypeOf(singleString).toEqualTypeOf<Tuple<[string]>>()

    expectTypeOf(singleString.concat('')).toEqualTypeOf<
      Tuple<[string, string]>
    >()

    expectTypeOf(singleString.concat([''] as const)).toMatchTypeOf<
      Tuple<[string, string]>
    >()
  })

  test('prepend is inferred properly', () => {
    const singleString = new Tuple('')

    expectTypeOf(singleString).toEqualTypeOf<Tuple<[string]>>()

    expectTypeOf(singleString.prepend('')).toEqualTypeOf<
      Tuple<[string, string]>
    >()

    expectTypeOf(singleString.prepend([''] as const)).toMatchTypeOf<
      Tuple<[string, string]>
    >()
  })

  test('push must match existing items', () => {
    const stringTuple = new Tuple('')

    expectTypeOf(stringTuple.push).toBeCallableWith('')

    expectTypeOf(stringTuple.push).parameter(0).not.toBeNumber()
  })

  test('Tuples can be combined', () => {
    const stringTuple = new Tuple('')

    const numberTuple = new Tuple(0, 1)

    expectTypeOf(stringTuple.concat(numberTuple)).toEqualTypeOf<
      Tuple<[string, number, number]>
    >()

    expectTypeOf(stringTuple.prepend(numberTuple)).toEqualTypeOf<
      Tuple<[number, number, string]>
    >()

    expectTypeOf(numberTuple.concat(stringTuple)).toEqualTypeOf<
      Tuple<[number, number, string]>
    >()

    expectTypeOf(numberTuple.prepend(stringTuple)).toEqualTypeOf<
      Tuple<[string, number, number]>
    >()

    expectTypeOf(stringTuple.prepend(numberTuple)).not.toMatchTypeOf<
      Tuple<[string, number, number]>
    >()

    expectTypeOf(stringTuple.concat(numberTuple)).not.toMatchTypeOf<
      Tuple<[number, number, string]>
    >()
  })
})