Skip to content

No type mismatch for records with enum keys inside object literal with dynamic keyΒ #63655

Description

@droooney

πŸ”Ž Search Terms

enum dynamic key record

πŸ•— Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about records

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.3#code/C4TwDgpgBAglC8UAKBDATsAligNgHgCUIBjAezQBM8BpCEAGigDsBXAWwCMI0A+HgbgBQg0JCi0QCKAHIU0qAB8ZHaUMFkmAZ2BQA5hGASpACgCUALnF0EPGXLUbtUFJbiIA3oKhQA2vsN0ZgC6ltLaaJhMutL0ggC+-EA

πŸ’» Code

type A = Partial<Record<Key, number>>;

type Key = 'a' | 'b';

const getKey = (): Key => 'a';

const a: A = {
  [getKey()]: 'string',
};

πŸ™ Actual behavior

TS didn't report an error even though it should

πŸ™‚ Expected behavior

Error report about type mismatch inside the record field

Additional information about the issue

As far as I've investigated it doesn't happen when the key is not an enum (i.e. string). Also when the dynamic key is narrowed down to a single option the error does get reported. However, when there are at least 2 options no error is reported.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions