microsoft / microsoft/TypeScript

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

Aperta
#38,914 0 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Usa l’esempio proposto di src/components/Foo.ts e src/components/Foo.d.ts come punto di partenza per la riproduzione, inclusi gli import mostrati in src/components/Bar.ts. Per prima cosa, verifica come TypeScript risolve attualmente le dichiarazioni per un modulo relativo, quindi determina la visibilità prevista di Bar ed ExportedType. Il lavoro è completato quando il file di dichiarazione separato è associato a Foo.ts, preservando il comportamento di esportazione indicato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
28/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.