Generic Type Aliases

This topic guides you what are generic type aliases

Recommended for experienced programmers with basic knowledge of type checking however if you dont know anything about type aliases and generics heres a brief tutorial:

  • Type aliases - Simply just like a container with a name dedicated for types
    • To declare one: type customType = string | number
    • How to use the type? You can use it like any ordinary type in type casting and type annotation, type the name of your type alias in type casting or type annotation and it will appear there
    • export keyword - typing the export keyword before the type keyword makes it so other scripts can use the type if the type alias were to be in the modulescript, to use the type alias in other scripts any variable holding the returned values by require you need to use the variable’s name then using dot notation to access the type alias ExampleModule.ExampleType and use either or both for type annotations and type casting local variable: ExampleModule.ExampleType
  • Generics - When talking about Types Aliases, Generics are simply a placeholder type you can slot in another type to make it hold that type otherwise it holds its assigned default type (explained further in the topic)
  • Tuple - A list of values separated by comma but isnt a table
    • Example: "string1", 2, true, false
    • You cannot use them directly although in some certain conditions you can, such as in a return statement return "string1", 2, true, false

Generic Type Aliases

Generic Type Aliases are one kind of a Type Alias

They differ from typical type aliases because instead of strictly having the type alias to predefine the necessary types needed for its diverse use, you would fill in the placeholder types corresponding to its intended type just like calling a function and giving arguments to it

To make a generic type alias you add angle brackets <> directly next to the type alias label and fill it in with a tuple of generics (note that “generics” and “tuple” terms was explained beforehand)

Read the comments as it contains the important details here

type unionType<A, B> = A | B

--[[
the type alias now holds string | number since it is filled out here and the type alias
represents a union type of those generics, basically the generic "A" is now string because
i filled it manually here and second generic "B" is now number because i filled it with number
here and so it evaluates to string | number since the type alias is a union type of those generics
]]
local variable: unionType<string, number>

An ordinary generic type alias requires you to fill out the generics unless you assign Default Types to the generics, Default types are simply just predefined types assigned to generics to hold that type whenever the generic is not filled out

to assign a default type to a generic add an equal sign next to it and give it a type

type unionType<A, B=string> = A | B

--[[
generic "A" is explicitly number however generic "B" is now string
because the generic type was not filled out with one
]]
local variable: unionType<number>

Conclusion

Overview:

  • Tuple - A list of values seperates by commas individually
  • Generic - A placeholder value you can slot in another type with
  • Angle Brackets - <>
  • Default Types - A substitute or alternate preassigned type to the type alias if the generics are not filled out

Strongly appreciate any honest feedback or suggestions or questions about this guide

otherwise vote in this poll

  • Bad tutorial
  • Normal tutorial
  • Good tutorial
0 voters
3 Likes