Skip to content

Specific type revert to string with dynamic keyΒ #60896

Closed as not planned
Closed as not planned
@Zamralik

Description

@Zamralik

πŸ”Ž Search Terms

dynamic key type

πŸ•— Version & Regression Information

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

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.2#code/GYVwdgxgLglg9mABAUwB4EMC2AHANsgRgB4BpFVKZMAEwGdFaoAnGMAcwD4AKAa2QE8AXIhIAaRADd0uEMmHgeYOAHcwASmEBvRAG0+-RKxEBdeWEUqkAXwBQmmwEgmyKCCZJtegacnTZiKwBuG1sbUEhYBHIsPGQAJlJyShp6RhZ2bn1hMV8ZOUQFJVUNRAAlZAg4JmpScULLDjtHZ1d3RE99Hyk8gOCrIA

πŸ’» Code

function example1<K extends string>(key: K, value: unknown): { [key in K]: unknown }
{
	return { [key]: value };
}

function example2<K extends string>(key: K, value: unknown): Record<K, unknown>
{
	return { [key]: value };
}

πŸ™ Actual behavior

Type '{ [x: string]: unknown; }' is not assignable to type '{ [key in K]: unknown; }'.(2322)
Type '{ [x: string]: unknown; }' is not assignable to type 'Record<K, unknown>'.(2322)

The type of the key is K but reverts to string

πŸ™‚ Expected behavior

The type of the key is preserved as K.

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