luau icon indicating copy to clipboard operation
luau copied to clipboard

Bad error message when passing read property to non-read function

Open OverHash opened this issue 6 months ago • 1 comments

In the new solver, the following code:

--!strict
type Item = {
	age: number
}
type ReadonlyItem = {
	read age: number
}

local function birthday(arg: Item): () end

local doge: ReadonlyItem = {age = 41}
birthday(doge)

will produce the following error message today:

TypeError: Type 'number' could not be converted into 'number'

(or more generically, "Type 'T' could not converted into 'T').

This is because we pass a type (ReadonlyItem) with a 'readonly' key to a function that does not mark this as 'readonly' (Item). If we just used Item everywhere, rather than ReadonlyItem, there would be no errors.

This error message is obviously non-nonsensical (though the error as far as I can tell is correct). It would be a better experience if the message was more descriptive.

OverHash avatar May 26 '25 10:05 OverHash

The error message has improved quite a bit since I posted this issue, though it is still arguably not perfect. The latest error message reads as:

TypeError: Type 'ReadonlyItem' could not be converted into 'Item'; this is because accessing age results in number in the former type and number in the latter type, and number is not a subtype of number

OverHash avatar Sep 21 '25 06:09 OverHash