luau icon indicating copy to clipboard operation
luau copied to clipboard

Type checker fails to properly infer T from a union

Open melindatrace opened this issue 3 months ago • 1 comments

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).

melindatrace avatar Sep 23 '25 04:09 melindatrace

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.

melindatrace avatar Sep 23 '25 04:09 melindatrace