Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Inline if statement behaving different (type wise) from regular block form #1983

@melindatrace

Description

@melindatrace
--!strict

type Value<T> = {
	kind: "value",
	value: T
}

local function peekA<T>(state: Value<T> | T): T
	if typeof(state) == "table" and state.kind == "value" then -- OK
		return (state :: Value<T>).value :: T
	else
		return state :: T
	end
end

local function peekB<T>(state: Value<T> | T): T
	return if typeof(state) == "table" and state.kind == "value" -- TypeError: Key 'kind' is missing from 'T & table' in the type '(T & table) | Value<T>' Luau(1032)
		then (state :: Value<T>).value :: T
		else state :: T
end

Tested with Luau 0.689 release (new solver).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingnew solverThis issue is specific to the new solver.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions