Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect inference with nested table literal with union types #596

Closed
ghost opened this issue Jul 16, 2022 · 0 comments · Fixed by #607
Closed

Incorrect inference with nested table literal with union types #596

ghost opened this issue Jul 16, 2022 · 0 comments · Fixed by #607
Assignees
Labels
bug Something isn't working

Comments

@ghost
Copy link

ghost commented Jul 16, 2022

--!strict
type path = {[string]: {string?}}
local path_map: {[string]: path?} = {
	["outermost"] = {
		["exterior"] = {
			"first interior", -- This is incorrectly inferred as `string` instead of `string?`, and will warn.
			"second interior",
			"third interior",
		}
	}
}
Type Error: (3,1) Type 'path_map' could not be converted into '{| [string]: path? |}'
caused by:
  Property 'outermost' is not compatible. Type 'path?' could not be converted into '{ exterior: {string} }'
caused by:
  Not all union options are compatible. Type 'path' could not be converted into '{ exterior: {string} }'
caused by:
  Property 'exterior' is not compatible. Type '{string?}' could not be converted into '{string}'
caused by:
  Property '[indexer value]' is not compatible. Type 'string?' could not be converted into 'string'

As a workaround, "first interior" must be casted to string?, but "second interior" and "third interior" do not need to be casted.

--!strict
type path = {[string]: {string?}}
local path_map: {[string]: path?} = {
	["outermost"] = {
		["exterior"] = {
			"first interior" :: string?,
			"second interior",
			"third interior",
		}
	}
}

Another workaround is to define the path beforehand.

--!strict
type path = {[string]: {string?}}
local path: path = {
	["exterior"] = {
		"first interior",
		"second interior",
		"third interior",
	}
}
local path_map: {[string]: path?} = {
	["outermost"] = path,
}

If path_map has type {[string]: path}, this will not occur.

--!strict
type path = {[string]: {string?}}
local path_map: {[string]: path} = {
	["outermost"] = {
		["exterior"] = {
			"first interior",
			"second interior",
			"third interior",
		}
	}
}
@ghost ghost added the bug Something isn't working label Jul 16, 2022
@zeux zeux assigned alexmccord and vegorov-rbx and unassigned alexmccord Jul 16, 2022
@zeux zeux closed this as completed in #607 Jul 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

2 participants