GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Allow template string index signatures · Issue #43136 · microsoft/TypeScript

Allow template string index signatures #43136

New issue
Copy link
New issue
Copy link
Closed
Closed
Allow template string index signatures#43136
Copy link

Description

@cherryblossom000
Lauren Yim (cherryblossom000)
opened on Mar 8, 2021
Issue body actions

Suggestion

🔍 Search Terms

template string index signature number bigint

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Currently, only strings or numbers can be used as index signatures. Until template strings were added, mapped types could work around this:

// this won't work:
// type Foo = {[x: 'foo' | 'bar']: number}

// this does:
type Foo = {[K in 'foo' | 'bar']: number} // or alternatively Record<'foo' | 'bar', number>

However, when attempting to use mapped types for template string types, the type becomes {}:

type TemplateString = `${number}: ${string}`
// DoesntWork is {}
type DoesntWork = Record<TemplateString, number>

📃 Motivating Example

Say you had a UUID type:

type UUID = `${string}-${string}-${string}-${string}-${string}`

This feature would allow more type-safe index signatures (instead of simply Record<UUID, User>, for example):

interface User {
  username: string
  // ...
}

const users: Record<UUID, User>

users['01234567-89ab-cdef-0123-456789abcdef'] = {username: 'Alice'}

// Compile error
users['invalid'] = {username: 'Bob'}

// Compile error
const someUser = users['invalid']

I'll change this example if I or anyone else comes up with a better one.

💻 Use Cases

Discord uses snowflakes, which are 64-bit integers, for IDs. The package discord-api-types exports this:

export type Snowflake = `${bigint}`

My use case is that I want to have a Record<Snowflake, SomeType>, but I was a little surprised and disappointed when that turned out to be {}.

An alternative that would fix my specific use case would be to allow bigints in index signatures, but in my opinion that would be confusing as property keys are converted to strings anyway. I believe allowing template strings in index signatures is a more general solution that could be helpful in many use cases.

Activity

  1. MartinJohns commented on Mar 8, 2021

    @MartinJohns
    Martin Johns (MartinJohns)
    on Mar 8, 2021
    Contributor
    More actions

    See #26797. It's on the roadmap for 4.3 as "Generalized index signatures".

  2. Lauren Yim (cherryblossom000)
    closed this as completedon Mar 8, 2021
  3. cherryblossom000 commented on Mar 8, 2021

    @cherryblossom000
    Lauren Yim (cherryblossom000)
    on Mar 8, 2021
    ContributorAuthor
    More actions

    Sorry, I didn't realise template string types were a part of that PR.

  4. microsoft
    locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.