Type unions of intersections cannot be coalesced when both sides of the union are the same

Reproduction Steps

Open a new place, copy/paste this source into a script.

Source Code
--!strict

local n: (number|string) & (number|string) = 5;

--[[

Type Error: (13,1) Type     '(number | string) & (number | string)'
could not be converted into 'number | string';
none of the union options are compatible

(spacing mine)

--]]

local m: (number|string) = n;

This is a pared down example. To be honest, I’m not entirely sure how I got into this situation. I was working with some code that I wrote when I was first getting the hang of Luau, so there might be some wonkiness there. Still, it seems that types of form (X|Y)&(X|Y) should be convertible to (X|Y)

Expected Behavior

I expect type (number|string)&(number|string) to be convertable to type (number|string).

Actual Behavior

I get a typechecking error.

image

Workaround

As always, the :: any escape hatch.

Issue Area: Studio
Issue Type: Other
Impact: Moderate
Frequency: Very Rarely

Also a curiosity: number & number can be converted to number just fine, so this issue seems specific to type intersections.

We have a fix for this ready, it will be included in a future update.

2 Likes

Hello.
Wanted to confirm that the fix made it to that ‘future update’ somewhere at the end of last year.