microsoft / microsoft/TypeScript

Index signature alternative

Aperta
#32,652 2 commenti 0 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

Search Terms

  • Index signature alternative
  • Index signature

Suggestion

An alternative of Index signature would be beneficial when you want to enforce, that all properties of an object must follow given pattern, without the need of [key: string | number]: T1.

My suggestion is to enable the use of ? for index signatures to mark them as type guards only.

type posParams = {
  [name?: string]: UrlSegment;
};

Use Cases

Whenever there is a need for an object with unknown keys, but known value types this would come in handy. A lot of people trying to implement some sort of callback that requires the index signature fails.The enormous number of results if one googles Index signature is missing in type confirms this

I personally tend to confront this problem regularly when working with frameworks..

Examples

For this example, I will use Angular, but this is applicable to any typescript code.

type UrlMatchResult = {
  consumed: UrlSegment[];
  posParams?: {
    [name?: string]: UrlSegment;
  };
};

When one would be to implement it with only specific posParams it would only check for the type of the object keys and values.

export interface IUrlParameters {
	baseFolder: UrlSegment;
	currentFolder?: UrlSegment;
}

export function folderMatcher(segments: UrlSegment[]): UrlMatchResult {
  const posParams: IUrlParameters  = {
    currentFolder: segments[0],
  };

  return {
    consumed: segments,
    posParams,
  }
}

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, etc.)
  • [-] This feature would agree with the rest of TypeScript's Design Goals.

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

La issue propone una sintassi opzionale per le firme di indice per verificare i valori delle proprietà note, con esempi che usano UrlSegment e Angular. Non vengono indicati file di implementazione, entry point o test; inizia individuando i test di controllo dei tipi delle firme di indice e del parser di TypeScript. Il lavoro è completo quando si è raggiunta una decisione di progettazione e il comportamento accettato è coperto da test.

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
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.