Typeforge: Common & Useful Type Utilities

Typeforge

Docs​​ ​ ​ ​ ​ ​ ​ ​ ​​​Github​​ ​ ​ ​ ​ ​ ​ ​ ​​​Wally

Typeforge is a collection of type function utilities intended to speed up development in type-checked code bases. It currently includes 34 type functions.


Why?

Type-checking helps to catch logical bugs early by ensuring values are used in ways consistent with their expected types, before the code ever runs.


Why Typeforge?

Unlike languages such as Typescript, Luau doesn’t come with many commonly used type utilities such as Partial, Pick and Params . This library exists to fill that gap.

Typeforge is robust and thoroughly unit tested:

  • Table’s read and write fields and indexers are properly respected.
    (Most other implementations of type functions included in this library don’t cover edge cases of the write value or indexer not being equal to the read value or indexer).
  • Each individual type function has around 5 - 11 tests to ensure behavior is correct and doesn’t regress.

Examples

Omit

Returns a subset of a type without specified components / properties.

type Result = Omit<
    "hello" | "world" | "foo" | "bar",
    "world" | "bar"
>

-- type Result = "foo" | "hello"

Clean

Removes duplicate components / properties from a type.

type Result = Clean<{
    age: number | number,
    [boolean]: boolean | boolean
}>

--[[
    type Result = {
        [boolean]: boolean,
        age: number
    }
]]

DeepPartial

Makes every property (including nested ones) in a table optional.

type Result = DeepPartial<{ hello: "world", foo: { bar: "baz" } }>

-- type Result = { foo: { bar: "baz"? }?, hello: "world"? }
1 Like