π 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.
π 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
π 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.