teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.04k stars 101 forks source link

missing narrowing type when not simple expression #760

Open fperrad opened 1 week ago

fperrad commented 1 week ago
local function bar(v: any): any
   if not v is {string:any} or not v.field then
      return
   end
   return v.field  -- Error: cannot index key 'field' in any 'v' of type <any type>
end

local function baz(v: any): any
   if not v is {string:any} then
      return
   end
   if not v.field then
      return
   end
   return v.field  -- OK: v is {string:any}
end

The code of these 2 functions is equivalent. But with a complex expression (ie. with a or), there is no narrowing type.