microsoft / microsoft/TypeScript
Relax visibility rules for type-aliases when 'declaration' compiler option is set.
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
One common thing I use type-aliases for is to give shorter, more expressive names to commonly used types throughout a file.
This is especially useful for callback signatures that see common use throughout a module, as well as string-literal types used in a union type, like type HttpMethods = 'get' | 'post' | 'put' | 'delete'.
Let's look at the following simplified example:
type CharCallback = (c: string, i: number) => string;
export function mapOnChar(str: string, fn: CharCallback): string {
const newStr = [];
for (let i = 0, lim = str.length; i < lim; i++)
newStr.push(fn(str.charAt(i), i));
return newStr.join('');
}
When using the declaration compiler option, this example fails to compile with the following error:
error TS4078: Parameter 'fn' of exported function has or is using private name 'CharCallback'.
This initially makes sense; the type-alias is not being exported so it is private. However, if you look at the type-alias, the only thing "private" about it is the name it was given, and an alias' name is not really important to or needed for a definition file.
There is no reason that the un-exported type-alias in the example cannot be automatically de-aliased back into (c: string, i: number) => string and that used in its place when the definition file is emitted.
However, if the alias in the example were to be exported, then it should not be de-aliased in the definition file.
I should point out that this suggestion is considering type-aliases only. If the CharCallback type was re-written as interface CharCallback { (c: string, i: number): string }, then that would be a good case to raise an error. An interface is generally considered more "concrete" than a simple type-alias, and so its name should be preserved and used in the definition file.
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 el ejemplo de TypeScript reportado que usa un alias CharCallback no exportado, una función exportada y la opción del compilador de declaraciones. Sigue la emisión de declaraciones y la comprobación de visibilidad TS4078; se considera terminado cuando los alias no exportados puedan insertarse inline en las declaraciones emitidas, mientras que los alias exportados sigan teniendo nombre y el caso de interface continúe siendo rechazado.
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