Add short example of width subtyping

This commit is contained in:
ajeffrey@roblox.com 2022-03-31 18:26:58 -05:00
parent 20308bed20
commit 7a7bed1e40

View file

@ -137,6 +137,15 @@ local t = {x = 1} -- {x: number}
t.y = 2 -- not ok
```
Sealed tables support *width subtyping*, which allows a table with more properties to be used as a table with fewer
```lua
type Point1D = { x : number }
type Point2D = { x : number, y : number }
local p : Point2D = { x = 5, y = 37 }
local q : Point1D = p -- ok because Point2D has more properties than Point1D
```
### Generic tables
This typically occurs when the symbol does not have any annotated types or were not inferred anything concrete. In this case, when you index on a parameter, you're requesting that there is a table with a matching interface.