Skip to content

Commit 2327e42

Browse files
v3.0.0
1 parent e7569c9 commit 2327e42

4 files changed

Lines changed: 59 additions & 38 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# v3.0.0
2+
- Now documenting changes via a changelog.
3+
- Fixed type errors.

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# Typeforge
2-
A Type Function utility library for Luau.
2+
A Type Function utility library for Luau.
3+
4+
docs: http://typeforge.luau.page/
5+
wally: https://wally.run/package/cameronpcampbell/typeforge

src/init.luau

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,11 @@ type function is_primitive(input_tag: string)
7373
input_tag == "string"
7474
end
7575

76-
type function unions_and_intersections_flatten(
77-
input: typeof(types.unionof()) | typeof(types.intersectionof())
78-
)
76+
type function unions_and_intersections_flatten(input: type)
7977
local input_tag = input.tag
8078

8179
if input_tag == "union" or input_tag == "intersection" then
82-
local components = input:components()
80+
local components: { type } = input:components()
8381

8482
for idx = #components, 1, -1 do
8583
local component = components[idx]
@@ -88,7 +86,7 @@ type function unions_and_intersections_flatten(
8886

8987
local component = unions_and_intersections_flatten(component)
9088
if component.tag == input_tag then
91-
local sub_components = component:components()
89+
local sub_components: { type } = component:components()
9290

9391
components[idx] = sub_components[1]
9492
for idx = 2, #sub_components do
@@ -220,7 +218,7 @@ type function stringify_table(
220218

221219
-- Sorts the table keys so its stringified representation
222220
-- to ensure stringification is deterministic.
223-
local keys = {}
221+
local keys: { type } = {}
224222
for key in properties do table.insert(keys, key) end
225223
table.sort(keys, function(a, b) return tostring(a) > tostring(b) end)
226224

@@ -264,7 +262,7 @@ type function stringify_table(
264262
end
265263

266264
type function stringify_components(
267-
components: { [number]: type },
265+
components: { type },
268266
concatenator: string
269267
)
270268
for idx, component in components do
@@ -402,7 +400,7 @@ end
402400

403401
type function hashset_insert(
404402
hashset: typeof(types.newtable()),
405-
non_hashsettable: { [number]: type },
403+
non_hashsettable: { type },
406404
input: type,
407405
input_tag: string
408406
)
@@ -416,7 +414,7 @@ end
416414

417415
type function hashset_has_type(
418416
hashset: typeof(types.newtable()),
419-
non_hashsettable: { [number]: type },
417+
non_hashsettable: { type },
420418
input: type,
421419
input_tag: string
422420
)
@@ -432,7 +430,7 @@ type function hashset_has_type(
432430
end
433431
end
434432

435-
type function hashset_from_components(input: { [number]: type })
433+
type function hashset_from_components(input: { type })
436434
local hashset = types.newtable()
437435

438436
local input_len = #input
@@ -455,9 +453,9 @@ end
455453

456454
--> Components Helpers -----------------------------------------------------------------
457455
type function components_filter(
458-
input: { [number]: type },
456+
input: { type },
459457
as: "union" | "intersection",
460-
as_builder: (components: { [number]: type }) -> (typeof(types.unionof()) | typeof(types.intersectionof())),
458+
as_builder: (components: { type }) -> (typeof(types.unionof()) | typeof(types.intersectionof())),
461459
callback: (component: type) -> boolean
462460
)
463461
local input_len = #input
@@ -485,9 +483,9 @@ type function components_filter(
485483
end
486484

487485
type function components_flatten(
488-
input: { [number]: type },
486+
input: { type },
489487
as: "union" | "intersection",
490-
as_builder: (components: { [number]: type }) -> (typeof(types.unionof()) | typeof(types.intersectionof()))
488+
as_builder: (components: { type }) -> (typeof(types.unionof()) | typeof(types.intersectionof()))
491489
)
492490
for idx = #input, 1, -1 do
493491
local component = input[idx]
@@ -513,7 +511,7 @@ type function components_flatten(
513511
end
514512

515513
type function components_map(
516-
input: { [number]: type },
514+
input: { type },
517515
callback: (value: type, ...any) -> any,
518516
...: any
519517
)
@@ -524,11 +522,11 @@ type function components_map(
524522
end
525523

526524
type function components_clean(
527-
input: { [number]: type },
525+
input: { type },
528526
as: "union" | "intersection",
529-
as_builder: (components: { [number]: type }) -> (typeof(types.unionof()) | typeof(types.intersectionof())),
527+
as_builder: (components: { type }) -> (typeof(types.unionof()) | typeof(types.intersectionof())),
530528
hashset: typeof(types.newtable()),
531-
non_hashsettable: { [number]: type }
529+
non_hashsettable: { type }
532530
)
533531
local input_len = #input
534532
for idx = input_len, 1, -1 do
@@ -573,15 +571,15 @@ type function components_clean(
573571
end
574572

575573
type function components_clean_unions_and_intersections(
576-
input: { [number]: type },
574+
input: { type },
577575
hashset: typeof(types.newtable()),
578-
non_hashsettable: { [number]: type },
576+
non_hashsettable: { type },
579577

580578
input_tag: string,
581579
input_tag_other: string,
582580

583-
input_builder: (components: { [number]: type }) -> (typeof(types.unionof()) | typeof(types.intersectionof())),
584-
input_builder_other: (components: { [number]: type }) -> (typeof(types.unionof()) | typeof(types.intersectionof()))
581+
input_builder: (components: { type }) -> (typeof(types.unionof()) | typeof(types.intersectionof())),
582+
input_builder_other: (components: { type }) -> (typeof(types.unionof()) | typeof(types.intersectionof()))
585583
)
586584
local input_len = #input
587585
for idx = input_len, 1, -1 do
@@ -625,7 +623,7 @@ type function components_clean_unions_and_intersections(
625623
end
626624

627625
type function components_find(
628-
input: { [number]: type },
626+
input: { type },
629627
as: "union" | "intersection",
630628
callback: (component: type, component_tag: string) -> boolean
631629
): boolean
@@ -642,7 +640,7 @@ type function components_find(
642640
end
643641

644642
type function components_find_type(
645-
input: { [number]: type },
643+
input: { type },
646644
as: "union" | "intersection",
647645
tag_to_find: string
648646
): boolean
@@ -651,7 +649,7 @@ end
651649
----------------------------------------------------------------------------------------
652650

653651
--> Union Helpers ----------------------------------------------------------------------
654-
type function union_from_components(input: { [number]: type })
652+
type function union_from_components(input: { type })
655653
local input_len = #input
656654
return if input_len == 0 then nil elseif input_len == 1 then input[1] else types.unionof(table.unpack(input))
657655
end
@@ -666,7 +664,7 @@ end
666664
----------------------------------------------------------------------------------------
667665

668666
--> Intersection Helpers ---------------------------------------------------------------
669-
type function intersection_from_components(input: { [number]: type })
667+
type function intersection_from_components(input: { type })
670668
local input_len = #input
671669
return if input_len == 0 then nil elseif input_len == 1 then input[1] else types.intersectionof(table.unpack(input))
672670
end
@@ -1774,14 +1772,14 @@ export type function FlattenTable(input: type)
17741772
local output: typeof(types.newtable()), output_idx: number
17751773
local can_add_read_indexer, can_add_write_indexer
17761774

1777-
local output_union_components: { [number]: type } = {}
1778-
local output_non_table_intersection_components: { [number]: type } = {}
1775+
local output_union_components: { type } = {}
1776+
local output_non_table_intersection_components: { type } = {}
17791777

17801778
for idx, component in components do
17811779
local component_tag = component.tag
17821780

17831781
if component_tag == "union" then
1784-
for _, sub_component in Flatten(component):components() do
1782+
for _, sub_component: type in Flatten(component):components() do
17851783
table.insert(
17861784
output_union_components,
17871785
sub_component
@@ -1840,7 +1838,7 @@ export type function FlattenTable(input: type)
18401838
local component_tag = component.tag
18411839

18421840
if component_tag == "union" then
1843-
for _, sub_component in Flatten(component):components() do
1841+
for _, sub_component: type in Flatten(component):components() do
18441842
table.insert(
18451843
output_union_components,
18461844
sub_component

src/init.test.luau

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ type CleanUnion_TestA = t.Expect<
268268
-- Test B: Cleans a nested union.
269269
type CleanUnion_TestB = t.Expect<
270270
t.CleanUnion<"hello" | "hello" | ("lol" | ("kek" | "kek" | "food"))>,
271-
"hello" | ("lol" | ("kek" | "food"))
271+
272+
-- types.unionof and types.intersectionof no longer support nesting.
273+
"hello" | "lol" | "kek" | "food"
272274
>
273275

274276
-- Test C: Cleans an intersection of unions.
@@ -280,13 +282,17 @@ type CleanUnion_TestC = t.Expect<
280282
-- Test D: Cleans a negated union.
281283
type CleanUnion_TestD = t.Expect<
282284
t.CleanUnion<t.Negate<(string | "foo") | (string | "bar")>>,
283-
t.Negate<"foo" | (string | "bar")>
285+
286+
-- types.unionof and types.intersectionof no longer support nesting.
287+
t.Negate<"foo" | string | "bar">
284288
>
285289

286290
-- Test E: Cleans a union between a type and a negated type.
287291
type CleanUnion_TestE = t.Expect<
288292
t.CleanUnion<string | "foo" | t.Negate<string | ("bar" | "baz")>>,
289-
"foo" | t.Negate<string | ("bar" | "baz")>
293+
294+
-- types.unionof and types.intersectionof no longer support nesting.
295+
"foo" | t.Negate<string | "bar" | "baz">
290296
>
291297
-------------------------------------------------------
292298
-------------------------------------------------------------------------------------------------------
@@ -411,7 +417,9 @@ type CleanIntersection_TestA = t.Expect<
411417
-- Test B: Cleans a nested intersection.
412418
type CleanIntersection_TestB = t.Expect<
413419
t.CleanIntersection<"hello" & "hello" & ("lol" & ("kek" & "kek" & "food"))>,
414-
"hello" & ("lol" & ("kek" & "food"))
420+
421+
-- types.unionof and types.intersectionof no longer support nesting.
422+
"hello" & "lol" & "kek" & "food"
415423
>
416424

417425
-- Test C: Cleans a union of intersections.
@@ -423,7 +431,9 @@ type CleanIntersection_TestC = t.Expect<
423431
-- Test D: Cleans a negated intersection.
424432
type CleanIntersection_TestD = t.Expect<
425433
t.CleanIntersection<t.Negate<(string & "foo") & (string & "bar")>>,
426-
t.Negate<"foo" & (string & "bar")>
434+
435+
-- types.unionof and types.intersectionof no longer support nesting.
436+
t.Negate<"foo" & string & "bar">
427437
>
428438

429439
-- Test E: Cleans an intersection between a type and a negated type.
@@ -709,7 +719,9 @@ type FlattenTable_TestK = t.Expect<
709719
-- TestL: Flattening a union of an intersection of tables with nested tables.
710720
type FlattenTable_TestL = t.Expect<
711721
t.FlattenTable<(({ hello: "world" } & { foo: "bar" }) | ({ baz: "bix" } | ({ lol: "kek" } | { right: "wrong" })))>,
712-
(({ hello: "world", foo: "bar" }) | ({ baz: "bix" } | ({ lol: "kek" } | { right: "wrong" })))
722+
723+
-- types.unionof and types.intersectionof no longer support nesting.
724+
{ hello: "world", foo: "bar" } | { baz: "bix" } | { lol: "kek" } | { right: "wrong" }
713725
>
714726

715727
-- TestM: Flattening a intersection of an intersection of tables with one table.
@@ -1268,4 +1280,9 @@ type Returns_TestD = t.Expect<
12681280
}
12691281
>
12701282
--------------------------------------------------------
1271-
-------------------------------------------------------------------------------------------------------
1283+
-------------------------------------------------------------------------------------------------------
1284+
1285+
1286+
type Foo = ("hello" | "goodbye") | "foo"
1287+
1288+
type Test = t.Inspect<Foo>

0 commit comments

Comments
 (0)