Luau Type Solver Fails to Properly Cast Literal Types as Keys in Primitive Dictionary

The Luau type solver does not correctly register literal types when used as table keys.

Specifically, when defining a table with {[Literals]: someType}, where Literals is a union of string literals, the type inference does not properly recognise Literals as valid keys.

This leads to an inability to assign a type to dictionary values without overwriting any existing dictionary keys.
This issue also occurs in --!strict, --!nonstrict and persist even with the new beta luau type solver

Reproduction

type Literals = 'exampleKey' | 'someOtherKey'

local Dictionary: {[Literals]: someRandomType}

Additionally, this bug prevents defining a generic dictionary type with literal string keys, making the following type impossible

type someDict<UnionOfLiteralStrings> = {[UnionOfLiteralStrings]: someRandomType}

Expected behavior

I expected Literals to be properly recognised as Literals and not as a string

4 Likes

Hello! Thank you for the report. Reading over Help with Typed Luau and Dictionaries - #26 by MinecoIII2, it sounds like the bug is:

--!strict

export type Argument = {
	description: string,
	parse: () -> (),
}

type ArgumentType = "player" | "color" | "speed"

local Args:{[ArgumentType]: Argument} = {
	["player"] = {
		description = "",
		parse = function() end,
	},

	["color"] = {
		description = "",
		parse = function() end,
	},

	["speed"] = {
		description = "",
		parse = function() end,
	},
}

local x = Args.| -- expect "player", "color", and "speed" to show up as autocomplete options here

return Args

If so, that sounds pretty reasonable to me.

Thank you for your response! That is indeed the bug, yes. To clarify, are you saying that this behavior is expected and not a bug?

To clarify, are you saying that this behavior is expected and not a bug?

I would describe this as a bug, and the specific case you cite should probably be fixed.

2 Likes