microsoft / microsoft/TypeScript

Suggestion: Allow merging of a wildcard import alias with a compile-time-only type alias of the same name

Abierto
#19,400 3 comentarios 6 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

For projects with a lot of types with their own sets of functions, namespacing is of course a standard technique in programming to resolve naming conflicts between the functions of those types. The problem with namespacing in TypeScript (and JavaScript, by extension) is that it introduces an object-property hierarchy that can't be easily normalized to eliminate dead code and unused functions and classes.

ES2015 module syntax provided a nice solution for this - wildcard imports - but unfortunately that doesn't play nicely with TypeScript's merging of namespaces with types. I'd love to see a way to associate a module with an interface or type alias without introducing the compiled output overhead of TypeScript namespaces when those namespaces exist only to associate some functions with a related type name.

Proposal

If a compile-time-only type is imported with the same alias as the wildcard alias, either as default or otherwise, that type name would automatically merge with the wildcard alias, allowing the wildcard alias to also be used to reference the merged type alias:

// foo.ts
export interface Foo { /* ... */ }
export function create(): Foo { /* ... */ }
export default Foo;

// bar.ts
import Foo, * as Foo from './foo';
const foo: Foo = Foo.create();

// ------
// or:

// bar.ts
import * as Foo from './foo';
import { Foo } from './foo';
const foo: Foo = Foo.create();
Rationale

This is sort of already supported with TypeScript namespace merging, but TypeScript namespaces generate extra boilerplate that can't be easily tree-shaken at build time, and for which the indirection is baked into the final output, unlike wildcard imports, the indirection of which is discarded at build time by modern bundlers.

I can't do this:

import * as SortedSet from '@collectable/sorted-set';

// redundant/contrived, just for explanation purposes:
function add<T>(value: T, set: SortedSet<T>): SortedSet<T> {
  return SortedSet.add(value, set);
}

or this:

// ... the names can't be merged here:

import SortedSet, * as SortedSet from '@collectable/sorted-set';

// or here:

import * as SortedSet from '@collectable/sorted-set';
import SortedSet from '@collectable/sorted-set';

// or here:

import * as SortedSet from '@collectable/sorted-set';
import { SortedSet } from '@collectable/sorted-set';

Instead, I'd have to do this:

import * as SortedSet from '@collectable/sorted-set';

function add<T>(value: T, set: SortedSet.SortedSet<T>): SortedSet.SortedSet<T> {
  return SortedSet.add(value, set);
}

Or I could give SortedSet some kind of general type name, but this would then be too abstract if not performing a wildcard import:

import * as SortedSet from '@collectable/sorted-set';

function add<T>(value: T, set: SortedSet.Type<T>): SortedSet.Type<T> {
  return SortedSet.add(value, set);
}

// but now I'm forced to manually alias the type at the point of consumption:

import {Type as SortedSet, add} from '@collectable/sorted-set';

function add<T>(value: T, set: SortedSet<T>): SortedSet<T> {
  return add(value, set);
}

Alternatively I could export an alias of the SortedSet type as Type, but now I'm confusing the issue by having two different type names that mean the same thing.

I'm open to any solution which lets me associate a module with an interface or type alias; I just want the output to be optimal for ES2015, rather than optimal for TypeScript. For libraries that export a number of different type modules, where those modules implement common function names, this would be a very useful addition.

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

Comienza revisando los ejemplos de la propuesta sobre importaciones wildcard y alias de tipo que solo existen en tiempo de compilación; después, sigue cómo TypeScript gestiona actualmente la combinación de importaciones de namespace, de tipo, default y con nombre. Se considera terminado cuando el comportamiento del lenguaje está especificado e implementado sin la sobrecarga de tiempo de ejecución no deseada de los namespace, y las pruebas cubren los ejemplos y una salida orientada a ES2015.

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

Evaluación

Stack tecnológico
javascript, 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.