Passed
Push — main ( e37dd6...73e52d )
by Andrii
02:03
created

src/sandbox/ts.test.ts   A

Complexity

Total Complexity 16
Complexity/F 1.45

Size

Lines of Code 172
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 143
dl 0
loc 172
rs 10
c 0
b 0
f 0
wmc 16
mnd 5
bc 5
fnc 11
bpm 0.4545
cpm 1.4544
noi 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A ts.test.ts ➔ check1 0 2 1
A ts.test.ts ➔ checkC 0 1 1
A ts.test.ts ➔ checkR 0 1 1
A ts.test.ts ➔ _thising 0 4 2
A ts.test.ts ➔ thising 0 6 1
1
export {}
2
3
it("`in string` vs `:string`", () => {
4
  type In = {[k in string]: boolean}
5
  type Colon = {[k: string]: boolean}
6
  
7
  function check1(_: {some: boolean}) {}
8
  function checkR<K extends string>(_: {[k in K]: boolean}) {}
9
  function checkC(_: {[k: string]: boolean}) {}
10
11
  const input1: In = {}, input2: Colon = {}
12
  //@ts-expect-error
13
  check1(input1)
14
  //@ts-expect-error
15
  check1(input2)
16
  checkR(input1)
17
  checkR(input2)
18
  checkC(input1)
19
  checkC(input2)
20
21
}) 
22
23
describe("this", () => {
24
  type Context = {context: string}
25
  
26
  it.todo("`this?` is illegal")
27
28
  it("|void - NOPE", () => {  
29
    function thising(this: void | Context): typeof this extends Context ? true : null {
30
      //@ts-expect-error
31
      return this && 'context' in this
32
      ? true
33
      : null
34
    }
35
  
36
    const suite1 = thising()
37
    , suite2 = thising.bind({context: ""})()
38
    , suite1Check: Record<string, typeof suite1> = {
39
      "null": null,
40
      //@ts-expect-error
41
      "true": true
42
    }
43
    , suite2Check: Record<string, typeof suite2> = {
44
      //TODO #13 @ts-expect-error
45
      "null": null,
46
      //@ts-expect-error //TODO #13
47
      "true": true
48
    }
49
50
    expect({suite1Check, suite2Check}).toBeInstanceOf(Object)
51
  })
52
53
  it("|undefined - NOPE", () => {  
54
    function thising(this: undefined | Context): typeof this extends Context ? true : null {
55
      //@ts-expect-error
56
      return this && 'context' in this
57
      ? true
58
      : null
59
    }
60
  
61
    //@ts-expect-error //TODO #13 The 'this' context of type 'void' is not assignable
62
    const suite1 = thising()
63
    , suite2 = thising.bind({context: ""})()
64
    , suite1Check: Record<string, typeof suite1> = {
65
      "null": null,
66
      //@ts-expect-error
67
      "true": true
68
    }
69
    , suite2Check: Record<string, typeof suite2> = {
70
      //TODO #13 @ts-expect-error
71
      "null": null,
72
      //@ts-expect-error //TODO #13
73
      "true": true
74
    }
75
76
    expect({suite1Check, suite2Check}).toBeInstanceOf(Object)
77
  })
78
79
  it("|unknown - NOPE", () => {  
80
    function thising(this: unknown | Context): typeof this extends Context ? true : null {
81
      //@ts-expect-error
82
      return this && 'context' in this
83
      ? true
84
      : null
85
    }
86
  
87
    const suite1 = thising()
88
    , suite2 = thising.bind({context: ""})()
89
    , suite1Check: Record<string, typeof suite1> = {
90
      "null": null,
91
      //@ts-expect-error
92
      "true": true
93
    }
94
    , suite2Check: Record<string, typeof suite2> = {
95
      //TODO #13 @ts-expect-error
96
      "null": null,
97
      //@ts-expect-error //TODO #13
98
      "true": true
99
    }
100
101
    expect({suite1Check, suite2Check}).toBeInstanceOf(Object)
102
  })
103
104
  it("overload", () => {
105
    function thising(): null
106
    function thising(this: Context): true 
107
    function thising(this: void | Context) {
108
      return this && 'context' in this
109
      ? true
110
      : null
111
    }
112
  
113
    const suite1 = thising()
114
    , suite2 = thising.bind({context: ""})()
115
    , suite1Check: Record<string, typeof suite1> = {
116
      "null": null,
117
      //@ts-expect-error
118
      "true": true
119
    }
120
    , suite2Check: Record<string, typeof suite2> = {
121
      //@ts-expect-error
122
      "null": null,
123
      "true": true
124
    }
125
126
    expect({suite1Check, suite2Check}).toBeInstanceOf(Object)
127
  })
128
129
  it("throw variable - Nope", () => {
130
    function _thising(this: void | Context) {
131
      return this && 'context' in this
132
      ? true
133
      : null
134
    }
135
136
    //Same for ((this: Context) => true) | () => null)
137
    type Thising = (<T>(this: T) => T extends Context ? true : null)
138
139
    const thising = _thising as Thising
140
141
    const suite1 = thising()
142
    , binded = thising.bind({context: ""})
143
    , suite2 = binded()
144
    , suite1Check: Record<string, typeof suite1> = {
145
      "null": null,
146
      //@ts-expect-error
147
      "true": true
148
    }
149
    , suite2Check: Record<string, typeof suite2> = {
150
      //TODO #13 @ts-expect-error
151
      "null": null,
152
      //@ts-expect-error //TODO #13
153
      "true": true
154
    }
155
156
    expect({suite1Check, suite2Check}).toBeInstanceOf(Object)
157
  })
158
159
  it("idfn this", () => {
160
    function thising<T>(this: T) {
161
      return this
162
    }
163
164
    const suite = thising()
165
    , suiteCheck: Record<string, typeof suite> = {
166
      "void": undefined
167
    }
168
    
169
    expect({suiteCheck}).toBeInstanceOf(Object)
170
  })
171
})
172