In Luau, writing to a field from an intersection type errors, but reading does not

Issue Type: Other
Impact: Low
Frequency: Constantly
Date First Experienced: 2021-04-25 17:04:00 (-07:00)
Date Last Experienced: 2021-04-25 18:04:00 (-07:00)

Reproduction Steps:

type T = {
   x: number
};
type U = T & {
   y: number
};
function f(a: U)
   print(a.x);
   a.x = 5 -- error
end

Above shows that attempting to write to an intersected type makes the checker shout at the user; despite the given type intersecting T. Reading shows no issue, however.

It doesn’t matter whether the type being intersected is empty, the checker will still throw:

type T = {};
type U = T & {
	x: number,
	y: number
};
function f(a: U)
	print(a.x);
	a.x = 5 -- error
end

Expected Behavior:
The script should be clear from any underlines - regardless of my U type not having an explicitly-defined x field.

Actual Behavior:
Writing to a.x makes the checker respond with something among the lines of Expected type table, got 'T & {| y: number |}' instead.
In the editor:
image

Workaround:
Don’t use type intersection and instead define your fields all in one type. This isn’t a very great workaround.

2 Likes

Thanks for the report! We’ve filed a ticket to our internal database and we’ll follow up when we have an update for you.

4 Likes

We have a solution ready internally, we’ll post an update here when it’s released.

3 Likes

The fix has been released for Roblox Studio 0.477

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.