microsoft / microsoft/TypeScript

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

Aperta
#43,041 0 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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;
}

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando le issue e le pull request collegate, insieme agli esempi di keyof, proprietà numeriche e signature di indice presenti in questa issue. Determina le implicazioni di progettazione della conversione delle chiavi numeriche in stringhe e la dipendenza dichiarata dalle pattern literal index signatures. Il lavoro è completato quando l'opzione proposta produce i risultati keyof richiesti senza modificare il comportamento esistente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.