Skip to content

Commit 28aa1a7

Browse files
committed
refactor: enhance types
1 parent 698b974 commit 28aa1a7

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

lib/binary_parser.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,12 @@ type BitSize =
208208
| 31
209209
| 32;
210210

211-
const PRIMITIVE_SIZES: { [key in PrimitiveType]: number } = {
211+
const conforms =
212+
<T>() =>
213+
<U extends T>(value: U): U =>
214+
value;
215+
216+
const PRIMITIVE_SIZES = conforms<{ [key in PrimitiveType]: 1 | 2 | 4 | 8 }>()({
212217
uint8: 1,
213218
uint16le: 2,
214219
uint16be: 2,
@@ -227,9 +232,14 @@ const PRIMITIVE_SIZES: { [key in PrimitiveType]: number } = {
227232
floatbe: 4,
228233
doublele: 8,
229234
doublebe: 8,
230-
};
231-
232-
const PRIMITIVE_NAMES: { [key in PrimitiveType]: string } = {
235+
} as const);
236+
237+
const PRIMITIVE_NAMES = conforms<{
238+
[key in PrimitiveType]:
239+
| `${"Int" | "Uint"}${8 | 16 | 32}`
240+
| `Big${"Int" | "Uint"}64`
241+
| `Float${32 | 64}`;
242+
}>()({
233243
uint8: "Uint8",
234244
uint16le: "Uint16",
235245
uint16be: "Uint16",
@@ -248,9 +258,11 @@ const PRIMITIVE_NAMES: { [key in PrimitiveType]: string } = {
248258
floatbe: "Float32",
249259
doublele: "Float64",
250260
doublebe: "Float64",
251-
};
261+
} as const);
252262

253-
const PRIMITIVE_LITTLE_ENDIANS: { [key in PrimitiveType]: boolean } = {
263+
const PRIMITIVE_LITTLE_ENDIANS = conforms<{
264+
[key in PrimitiveType]: boolean;
265+
}>()({
254266
uint8: false,
255267
uint16le: true,
256268
uint16be: false,
@@ -269,7 +281,7 @@ const PRIMITIVE_LITTLE_ENDIANS: { [key in PrimitiveType]: boolean } = {
269281
floatbe: false,
270282
doublele: true,
271283
doublebe: false,
272-
};
284+
} as const);
273285

274286
type Next<O, N, T> = N extends string
275287
? Parser<

0 commit comments

Comments
 (0)