microsoft / microsoft/TypeScript

allow local module type declarations on separate files (similar to C .h files)

Abierto
#38,914 0 comentarios 3 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Awaiting More Feedback Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

Search Terms

.d.ts definitions ambient module typing local

Suggestion

I would like to being able to compose typescript modules/components inside my application .i.e. src/components/Foo.ts and being able to declare its typings under src/components/Foo.d.ts where typescript would either infer those types automatically.

Disclaimer: It may happen that this is already possible, but I already researched everywhere and read the documentation as well as trying to declare ambient modules (which won't work with relative paths), so I am trying this as a last resort in case it's indeed not currently possible.

Use Cases

Sometimes, when creating components inside an application, some may have a quite extensive type set which usually makes the file difficult to read and take the focus away from the implementation.
Like so:

src/component/Foo.ts
##########################
#         type           #
#         type           #
#         type           #
#         type           #
#         type           #
#                        #
#    implementation      #
#    implementation      #
##########################

This would make it easier for the developer to organise their code similar to C's .h files separating the definition from implementation.

Examples

Project structure:

src/components
src/components/Foo.ts
src/components/Foo.d.ts
src/components/complex/index.ts
src/components/complets/index.d.ts

types are automatically inferred within the main module

src/components/Foo.ts
const bar:Bar = {foo:1,bar:2};
export default ...
src/components/Foo.d.ts
type Bar = {foo:number; bar:number; }
export type ExportedType = string[];

types can still be imported from a different component

src/components/Bar.ts
import type {ExportedType} from './Foo';
import type {Bar} from './Foo'; // < Error as Bar is not exported

This would result in:

src/component/Foo.d.ts                    src/components/Foo.ts
##########################                ##########################
#         type           #                #    implementation      #
#         type           #                #    implementation      #
#         type           #                #                        #
#         type           #                #                        #                        
#         type           #                #                        #
##########################                ##########################

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

Usa el ejemplo propuesto de src/components/Foo.ts y src/components/Foo.d.ts como reproducción inicial, incluidos los imports mostrados en src/components/Bar.ts. Primero investiga cómo resuelve actualmente TypeScript las declaraciones de un módulo relativo y, después, determina la visibilidad esperada de Bar y ExportedType. Se considera terminado cuando el archivo de declaración independiente está asociado con Foo.ts, conservando el comportamiento de exportación indicado.

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.