How to use type systems?

I’ve looked at quite a few modules and frameworks, and I was going through the Knit framework module when I first encountered a type system.

From all I was able to gather, you create a type check by:

type thing={

}

local newthing: thing={}

Anyone mind explaining what’s going on here?

The official documentation is here: Type checking - Luau
Unfortunately I don’t have a good way to explain it in short because I don’t think it works well at all, not least because type checking is optional.
If you want to learn about type systems in general, I’d recommend learning about Rust’s: Data Types - The Rust Programming Language
It works very similarly, however, Rust is strongly typed, which means complying with the rules is required and there are no tricks you can do to break the rules.

1 Like

Alright thanks my friend, I’ll check these documentations out.

The philosophy surrounding type annotations is that you invest the time and effort of writing them in the hopes of them helping you catch silly coding mistakes in the future, thus saving you way more time and effort because you will be spending less time scratching your head on trivial problems. It will sound and feel tedious and redundant at first, but trust me, from experience, it is invaluable and extremely useful.

Type annotations is also integrated into the Roblox IntelliSense IDE so it can directly boost your productivity by suggesting the proper terms and stuff when you are coding; example:

You wouldn’t get that kind of suggestion without using type annotations! It even knows that because you gave the constructor a Part, that particular method will also return a Part.

But yeah please read the documentation, you definitely won’t understand everything at first because it’s literally like learning a new language. In fact, this is the code for the example above, see if you can understand it:

type class = {
	new: <T, U>(driver: T, ...U) -> Car<T, U>,
}
export type Car<T, U> = {
	driver: T,
	passengers: {U},
	
	GetDriver: (self: Car<T, U>) -> T,
	GetPassenger: (self: Car<T, U>, seat: number) -> U?,
}

local Car = {}
Car.__index = Car

function Car.new<T, U>(driver: T, ...: U): Car<T, U>
	local new = {
		driver = driver,
		passengers = {...},
	}
	return setmetatable(new, Car)::any
end

function Car.GetDriver<T, U>(self: Car<T, U>): T
	return self.driver
end

function Car.GetPassenger<T, U>(self: Car<T, U>, seat: number): U?
	return self.passengers[seat]
end

return (Car::any)::class
2 Likes

Seems like I definitely have to study the docs more. Thank you my friend!