Skip to content

Commit

Permalink
add type section
Browse files Browse the repository at this point in the history
  • Loading branch information
dgxo committed Jan 27, 2025
1 parent 823773b commit 93a79b6
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,39 @@ end
return FooThing
```

## Types
- Always give a type to uninitialised variables.
<p class="style-good">Good:</p>
```lua
local currentPosition: Vector3
```
<p class="style-bad">Bad:</p>
```lua
local currentPosition
```
- Use type annotations on all function arguments and return values.
<p class="style-good">Good:</p>
```lua
local function doSomething(position: Vector3): number
return position.X + position.Y + position.Z
end
```
<p class="style-bad">Bad:</p>
```lua
-- Here, when using auto-complete, no argument or return type is shown.
local function doSomething(position)
return position.x + position.y + position.z
end
```
<p class="style-exception">Exception:</p>
```lua
-- Although not recommended for readability, Luau's type system is smart enough to
-- infer the type of a function's return value from its return statement.
local function doSomething(a, b)
return a + b
end
```

## Yielding
Do not call yielding functions on the main task. Wrap them in `coroutine.wrap` or `task.delay`, and consider exposing a Promise or Promise-like async interface for your own functions.

Expand Down

0 comments on commit 93a79b6

Please sign in to comment.