Minor wording changes to typecheck.md

This commit is contained in:
Alexander McCord 2020-06-18 23:55:19 -07:00
parent aac31bdc1f
commit 149c14f115
1 changed files with 13 additions and 13 deletions

View File

@ -79,7 +79,7 @@ local a
local b = nil
```
## Functions
## Function types
Let's start with something simple.
@ -119,7 +119,7 @@ f(2) -- ok
f("foo") -- not ok
```
## Tables
## Table types
From the type checker perspective, each table can be in one of three states. They are: `unsealed table`, `sealed table`, and `generic table`. This is intended to represent how the table's type is allowed to change.
@ -273,34 +273,34 @@ When we check the type of a value, what we're doing is we're refining the type,
Using `type` comparison:
```lua
local x: string | number
local stringOrNumber: string | number = "foo"
if type(x) == "string" then
local y: string = x -- ok
local z: number = x -- not ok
local onlyString: string = stringOrNumber -- ok
local onlyNumber: number = stringOrNumber -- not ok
end
local y: string = x -- not ok
local z: number = x -- not ok
local onlyString: string = stringOrNumber -- not ok
local onlyNumber: number = stringOrNumber -- not ok
```
Using truthy test:
```lua
local x: string? = nil
local maybeString: string? = nil
if x then
local y: string = x -- ok
if maybeString then
local onlyString: string = maybeString -- ok
end
```
And using `assert` will work with the above type guards:
```lua
local x: string | number
local stringOrNumber: string | number = "foo"
assert(type(x) == "string")
local y: string = x -- ok
local z: number = x -- not ok
local onlyString: string = stringOrNumber -- ok
local onlyNumber: number = stringOrNumber -- not ok
```
## Roblox types