Not Understanding --!strict

Hey, i’ve been trying to understand and learn what does --!Strict is and what it do, but every tutorial, AI, and even Roblox Documentation makes it sound so complicated - There is always something i don’t understand.

If it sounds unclear or it has mistakes - my bad, I don’t speak English that often.

Thank you

since you have not asked any specific question, check these two samples:

local function p(v)
	print("p", v)
	
	if true then
		return false
	end
end

p(1)
p("1")
p({1})

and

--!strict

local function p(v: number): boolean
	print("p", v)
	
	if true then
		return false
	end
end -- Type Error: (9,1) Not all codepaths in this function return 'boolean'.

p(1)
p("1") -- Type Error: (12,3) Expected this to be 'number', but got 'string'
p({1}) -- Type Error: (13,3) Expected this to be 'number', but got '{number}'

feel free to insert them to any script and check how they look.
what is worth noticing:

  • both works equally
  • second one has warnings because of --!strict
    image

Basically --!strict allows you to write more “types oriented” code and, as a result, have better awareness what types values have in the code.

or do you have anything particular on your mind?..

2 Likes

Basically.

There is something called typechecking which just means to check the type of any input and output you give/get from your code.

For example:

local number = 102

This is a number but it can be also a string? So how do we make sure it’ll always be a number? We set a type to it! Types are set by using a colon : symbol after the variable name. Like so:

local myNumber:number = 1230

Now, the variable will always be a number.

The -!strict keyword just makes the script editor make sure that you set types to EVERYTHING. If even one thing doesn’t have a type, it will show a warning. It’s mainly used by module developers to make module making easier. It’s also used in OOP and other stuff. If you’re an solo developer, you won’t use it. If you work in a big team however, you will probably use it.

Oh, so it like “Strictly checks types to make sure it matches, if it does - inform”. Right?

Thanks.
I really appreciate a big explanation

sort of.
Normal languages have type checking out-of-the-box, for example:

  • types must be provided for values always
  • types must be followed, otherwise it’s type error

This check is performed during compilation.

Script languages most often do not have type checking, which makes them poor choice tools for long term and large scale projects.

Seems that --!strict has been added to give developers the option to save the situation a little.

Thank you. i really appreciate the big texts and examples