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

View File

@ -79,7 +79,7 @@ local a
local b = nil local b = nil
``` ```
## Functions ## Function types
Let's start with something simple. Let's start with something simple.
@ -119,7 +119,7 @@ f(2) -- ok
f("foo") -- not 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. 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: Using `type` comparison:
```lua ```lua
local x: string | number local stringOrNumber: string | number = "foo"
if type(x) == "string" then if type(x) == "string" then
local y: string = x -- ok local onlyString: string = stringOrNumber -- ok
local z: number = x -- not ok local onlyNumber: number = stringOrNumber -- not ok
end end
local y: string = x -- not ok local onlyString: string = stringOrNumber -- not ok
local z: number = x -- not ok local onlyNumber: number = stringOrNumber -- not ok
``` ```
Using truthy test: Using truthy test:
```lua ```lua
local x: string? = nil local maybeString: string? = nil
if x then if maybeString then
local y: string = x -- ok local onlyString: string = maybeString -- ok
end end
``` ```
And using `assert` will work with the above type guards: And using `assert` will work with the above type guards:
```lua ```lua
local x: string | number local stringOrNumber: string | number = "foo"
assert(type(x) == "string") assert(type(x) == "string")
local y: string = x -- ok local onlyString: string = stringOrNumber -- ok
local z: number = x -- not ok local onlyNumber: number = stringOrNumber -- not ok
``` ```
## Roblox types ## Roblox types