/
shape.lua
196 lines (145 loc) · 3.64 KB
/
shape.lua
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
---@shape Shape1
---@field a number
---@type Shape1
local shape1
---@class NotShape1
---@field a number
local NotShape1
---@type NotShape1
local notShape1
shape1 = {a = 1}
notShape1 = {a = 1} -- Expect error
shape1 = notShape1
notShape1 = shape1 -- Expect error
shape1 = {a = "not a number"} -- Expect error
shape1 = {a = 1, extraMember = "whatever"}
---@shape Shape2 : Shape1
---@field b string
---@type Shape2
local shape2
shape2 = shape1 -- Expect error
shape1 = shape2
shape2 = {a = 2} -- Expect error
shape2 = {a = 2, b = "some string"}
shape2 = {a = 2, b = 2} -- Expect error
---@shape GenericShape<T>
---@field a T
---@type number
local aNumber
---@type string
local aString
---@type GenericShape<number>
local genericNumberShape
genericNumberShape = {a = aNumber}
genericNumberShape = {a = aString} -- Expect error
---@type GenericShape<string>
local genericStringShape
genericStringShape = {a = aNumber} -- Expect error
genericStringShape = {a = aString}
---@shape DerivedGenericShape<T> : GenericShape<T>
---@field b T
---@type DerivedGenericShape<number>
local derivedGenericShape = {a = 1, b = 2}
local notALiteral = {a = aString}
derivedGenericShape = {a = aNumber} -- Expect error
derivedGenericShape = {a = 1, b = aString} -- Expect error
derivedGenericShape = notALiteral -- Expect error
---@param shape DerivedGenericShape<string>
local function takesAShape(shape)
end
takesAShape({a = aString, b = aString})
takesAShape({a = aNumber, b = aNumber}) -- Expect error
---@shape NestedShape
---@field shape1 Shape1
---@field shape2 Shape2
---@type NestedShape
local nestedShape = {
shape1 = {a = aNumber},
shape2 = {a = aNumber, b = aString}
}
nestedShape = {
shape1 = {a = 1},
shape2 = (shape2)
}
nestedShape = {
shape1 = {a = aString}, -- Expect error
shape2 = {a = aNumber} -- Expect error
}
local notShape2 = {a = 1, b = 10}
---@type NestedShape
local nestedShape3 = {
shape1 = {a = aString, b = aString},
shape2 = {a = 1, b = 1}
}
---@return NestedShape
local function returnNestedShape()
return {
shape1 = {a = {}}, -- Expect error
shape2 = {a = aNumber, b = aNumber} -- Expect error
}
end
---@shape ShapeWithOptionalField
---@field requiredField number
---@field optional number|nil
---@type ShapeWithOptionalField
local shapeWithOptionalField = {
requiredField = 1
}
shapeWithOptionalField = {
requiredField = 1,
optional = aString -- Expect error
}
---@shape ArrayValue
---@field a number
---@shape WithArrayValues
---@field values ArrayValue[]
---@type WithArrayValues
local goodLiteral = {
values = {
{
a = 1
}
}
}
---@type WithArrayValues
local badLiteral = {
values = {
{
a = "some string" -- Expect error
}
}
}
---@shape StringNumberBooleanTuple
---@field [1] string
---@field [2] number
---@field [3] boolean
---@param tuple StringNumberBooleanTuple
local function testTuple(tuple) end
testTuple({"hi", 1, true})
testTuple({"hi", 1, 1}) -- Expect error
---@type number[]
local numberArray
---@shape NumberTuple
---@field [1] number
---@field [2] number
---@field [3] number
---@type NumberTuple
local numberTuple
numberArray = numberTuple
---@shape NonContiguousNumberTuple : NumberTuple
---@field [5] number
---@type NonContiguousNumberTuple
local nonContiguousNumberTuple
numberArray = nonContiguousNumberTuple -- Expect Error
---@shape PrimitiveAndTable
---@field primitiveField string
---@field tableField {}
---@type PrimitiveAndTable
local primitiveAndTable = {
primitiveField = "a string",
tableField = {}
}
primitiveAndTable = {
primitiveField = 1, -- Expect error
tableField = {}
}