Skip to content

Inconsistent behaviour of Record or Enum and mapped typeΒ #62853

@ipva3

Description

@ipva3

πŸ”Ž Search Terms

"mapped type" "record" "enum"

πŸ•— Version & Regression Information

  • This issue can be produced in versions >= 5.8.3 (Don't test lower versions)

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABFApgZygMXBA6jKACwCUUI4AnAE1KhArAAoBKALkVPOoB4MKYwAcwA0iPgMEA+RAG8AUIkQUUdBrIC+Abjnq55MBmToonSlUQBeIxmyR8RU9Vr0mzbXoSHUGAKJgQALYA8gBGAFaWsgqIAILsAOQAirg+APox8cLRAEIJyWnZ8TqIAIZoiPoY7lAAngAOKIgAKsZ+gaERVrUNcMDWUG3B4QDaANYoNb3I9ShT3gP+Q2EAuu6gkLAI-faEHYNojPPsjlTcLb6LHaLiQpJsYlD8QlGKyqpI8wB0+enaunLraDwD7GHYdACyJTq+0OxnYMkQwwA0hNEAJmq1LuFluwboJEOp7niXkoVC5+t8Ur8dHIPAYoP0AIyReZg8Iw+YnNyIAD0PMQADk4AzgCUYAAbRAhEAMgIgDB0rzGRmpZldUEEXbhSHQxYHTlkMzcvmITBiyUlMDmAjxcpcZTQWkoRYYi6BEkxSJJKkZLKKbJen6FGkAnCbEEYHYcuEcQ08c4LQLXR4SO64lPPeSvMlqL4-GJ-RUM+YAJhZGqI0YwXM0vP5MXFaDgiDlhlFEqAA

πŸ’» Code

function testFuncWithRecordReturn(): Record<string, string> {
  return {};
}
const testRecord = testFuncWithRecordReturn();

const testEnumObj = {
  A: 'QWE_A',
  B: 'QWE_B'
} as const;

type TestEnumObj = typeof testEnumObj[keyof typeof testEnumObj];

function testWithObjEnums(test: Record<TestEnumObj, string>): string {
  return test.QWE_A;
}

function testWithObjMapEnums(test: { [Key in TestEnumObj]: string }): string {
  return test.QWE_A;
}

const test1 = testWithObjEnums(testRecord); // Not fail but must
const test1_1 = testWithObjMapEnums(testRecord); // Fail and it's correct

enum TestEnum {
  A = 'QWE_A',
  B = 'QWE_B'
}

function testWithEnums(test: Record<TestEnum, string>): string {
  return test.QWE_A;
}
const test2 = testWithEnums(testRecord); // Also must fail

πŸ™ Actual behavior

Typescript does not fail in places with specific keys objects

πŸ™‚ Expected behavior

Typescript must shows an errors in commented places with same error as the correct one

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions