luau
luau copied to clipboard
Type checker fails to properly infer T from a union
I expect that variable b to be inferred as number, because that's what the T is.
--!strict
type Value<T> = {
kind: "value",
value: T
}
local function peek<T>(state: Value<T> | T): T
return if typeof(state) == "table" and state.kind == "value"
then (state :: Value<T>).value :: T
else state :: T
end
local a = peek(4) -- number
local b = peek({ kind = "value", value = 4 }) -- number | { kind: "value", value: 4 }
Tested with Luau 0.691 release (new solver).
I accidently have selected wrong option when filing a bug report, I would appreciate if someone with sufficient permissions could adjust the labels to reflect the nature of this issue.