From 7a7bed1e40f114b4e3c42c7b631c54f88b7f0e5b Mon Sep 17 00:00:00 2001 From: "ajeffrey@roblox.com" Date: Thu, 31 Mar 2022 18:26:58 -0500 Subject: [PATCH] Add short example of width subtyping --- docs/_pages/typecheck.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/_pages/typecheck.md b/docs/_pages/typecheck.md index 9443f112..20f73987 100644 --- a/docs/_pages/typecheck.md +++ b/docs/_pages/typecheck.md @@ -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.