Typecasting in Luau does not work with nested tables

This does not work:

type a = {
	m: {
		c: number
	};
}
type b = {
	m: {
		c: number;
		d: number?;
	}
}
-- Cannot cast 'a' into 'b' because the types are unrelated
local v = ((nil :: any) :: a) :: b

Yes this simpler case without nested tables works fine:

type a = {
	c: number
}
type b = {
	c: number;
	d: number?;
}
-- This is okay
local v = ((nil :: any) :: a) :: b
1 Like

We’ve filed a ticket to our internal database, and we’ll follow up when we have an update!

Thanks for the report!

1 Like

Hey there, just wanted to follow-up with an update on this issue! The Luau team is currently hard at work on implementing an approach called local type inference that will allow us to get more accurate typechecking in situations like this. This work is still ongoing, but I wanted to at least let you know that this issue (and really, a whole host of similar restrictions to Luau’s current typechecker) are being worked on and will hopefully be fixed in an upcoming release!

5 Likes