microsoft / microsoft/TypeScript

An alternative option to `keyofStringsOnly` that stringifies numeric properties and index signatures

Open
#43,041 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Suggestion

🔍 Search Terms

  • keyof
  • keyofStringsOnly

✅ 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

Since pattern literal types like `${number}` are valid as of https://github.com/microsoft/TypeScript/pull/40598, I feel like there should be an option to make:

type ArrayLikeKeys = keyof ArrayLike<any>;

result in:

type ArrayLikeKeys = "length" | `${number}`;

The same should happen for any numeric property key:

interface Foo {
	1: "a";
	2: "b";
	3: "c";
}

// Currently is: : 1 | 2 | 3
// Should be: "1" | "2" | "3"
type KeyOfFoo = keyof Foo;

// Currently is: never
type StrictKeyOfFoo = (keyof Foo) & (string | symbol);

like with:

interface Foo {
	"1": "a";
	"2": "b";
	"3": "c";
}

// Is: "1" | "2" | "3"
type KeyOfFoo = keyof Foo;

📃 Motivating Example

This makes keyof and numeric index signatures match runtime behaviour.

💻 Use Cases

Currently, it’s necessary to use the strictKeyof and StrictPropertyKey helpers:

type strictKeyof<T> = keyof T extends infer K
	? (K extends number ? `${K}` : K)
	: never;

type StrictPropertyKey = string | symbol;

Relevant issues:


This will most likely depend on https://github.com/microsoft/TypeScript/pull/26797, so that treating numeric index signatures as numeric pattern literal index signatures is valid:

interface ArrayLike<T> {
	readonly [index: `${number}`]: T;
	readonly length: number;
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the linked issues and pull requests, along with the examples for keyof, numeric properties, and index signatures in this issue. Determine the design implications of stringifying numeric keys and the stated dependency on pattern literal index signatures. Done means the proposed option produces the requested keyof results without changing existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.