Correct use of function with keyof still fails in strict mode

When using a function that implements the keyof type function, any proper use of singletons calling the function is incorrectly marked as wrong in --!strict mode.

The editor marks this as Type 'string' cannot be converted into "foo"
Expected behavior

If everything worked correctly, foo is a valid key of type Hello, so use of hi("foo") meets keyof<Hello>.

3 Likes

All literals are inferred to have the type of their base type, not their type literal.

"foo" being inferred as string instead of the type literal "foo" is intended and correct here, and that’s why it fails.

2 Likes

I still don’t think that this is intended because I don’t think Luau devs intended for everyone to have to spam "foo"::"foo" everywhere in their code.
Continuing,

the keyof example would fail if for that to be true.

Oh, and also, if I replaced my example with a regular union, the editor doesn’t complain. This isn’t intended.

1 Like

Thank you for the report! This looks like a regression in how we handle inference for primitive vs singleton types. When keyof was originally introduced, I added specific logic to influence singleton type inference when the expected type has keyof in it. If this just started happening recently, it might be related to some of the generalization changes we’re making, but most likely, the regression is somewhere in the handling of expected types. We’ll take a look as soon as possible, cheers! :slight_smile: