Skip to content

Object Indexing with a Template StringΒ #44675

Closed
@hbiede

Description

@hbiede

Bug Report

πŸ”Ž Search Terms

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

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

const fontSizes = {
  h1: 50,
  h2: 45,
  h3: 40,
  h4: 35,
  h5: 30,
  h6: 25,
}

type Props = {
  level: 1 | 2 | 3 | 4 | 5 | 6;
}

const getFontSizeForHeader = ({ level }: Props) => fontSizes[`h${level}`]; // error

console.log('Test', getFontSizeForHeader({ level: 1 }));

πŸ™ Actual behavior

When indexing an object with a template string, the template string is typed just as string instead of the proper union, causing an error and requiring a type cast or ts-ignore to rid.

πŸ™‚ Expected behavior

The template string typing to be used to check for valid indexing

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions