microsoft / microsoft/TypeScript

Index signature alternative

Abierto
#32,652 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Awaiting More Feedback Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

El issue propone una sintaxis opcional de firma de índice para comprobar los valores de propiedades conocidas, con ejemplos que usan UrlSegment y Angular. No se nombran archivos de implementación, puntos de entrada ni tests; empieza localizando las pruebas de comprobación de tipos de firmas de índice y del parser de TypeScript. Se considera terminado cuando se haya llegado a una decisión de diseño y se haya cubierto con tests el comportamiento aceptado.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.