microsoft / microsoft/TypeScript
[Feature Request] Preserve comments when using Extract<keyof T, string>
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
Search Terms
mapped type, preserve comment, keyof, Extract
Suggestion
type Mapped<T> = {
[k in keyof T]: ["some transformation", T[k]]
};
interface IFoo {
/** The string */
x: string;
/** The number */
y: number;
}
declare const mappedIFoo: Mapped<IFoo>;
//Tooltip shows "The string" as the comment
mappedIFoo.x
//////////////////////////
type Mapped2<T> = {
[k in Extract<keyof T, string>]: ["some transformation", T[k]]
};
declare const mapped2IFoo: Mapped2<IFoo>;
//Tooltip DOES NOT show "The string" as the comment
mapped2IFoo.x
I'd like it if the fields of the mapped type could somehow preserve the comments of the fields of T, even after using Extract<keyof T, string>.
Use Cases
In my projects, there are many cases where I only want to deal with string keys and not symbol|number keys. So, I use Extract<keyof T, string> a lot. However, this does not preserve comments and makes me sad =(
Examples
//--noImplicitAny
type Mapped<T extends { [k: string]: any }> = {
[k in keyof T]: ["some transformation", T[k]]
};
const someSymbol: unique symbol = Symbol();
interface IFoo {
/** The string */
x: string;
/** The number */
y: number;
1: "i am a number";
[someSymbol] : "i am a symbol"
}
declare const mappedIFoo: Mapped<IFoo>;
//Tooltip shows "The string" as the comment
mappedIFoo.x;
//Is allowed, because we use `keyof T`
mappedIFoo[1];
//Is allowed, because we use `keyof T`
mappedIFoo[someSymbol];
//////////////////////////
type Mapped2<T extends { [k : string] : any }> = {
[k in Extract<keyof T, string>]: ["some transformation", T[k]]
};
declare const mapped2IFoo: Mapped2<IFoo>;
//Expected: Tooltip shows "The string" as the comment
//Actual: Tooltip DOES NOT show "The string" as the comment
mapped2IFoo.x;
//Expected: is not allowed
//Actual : Is not allowed; OK!
mapped2IFoo[1];
//Expected: is not allowed
//Actual : Is not allowed; OK!
mapped2IFoo[someSymbol];
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
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con los ejemplos de TypeScript y los Playgrounds proporcionados, comparando el comportamiento de los tooltips de Mapped con Mapped2 mediante Extract<keyof T, string>. Se considera terminado cuando se conservan comentarios como “The string” y “The number” para los campos mapeados, mientras que las claves number y symbol siguen excluidas; no se nombran archivos ni tests del repositorio.
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
- 35/100