microsoft / microsoft/TypeScript

Documentation: usage of types/interfaces defined in modules on global

Aperta
#32,884 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Docs
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

  • export from module to global
  • global namespace
  • declare as global
  • assign to global
  • globalThis

Suggestion

Documentation says that

It can be surprisingly difficult to access or declare values in the global scope, perhaps because we’re writing our code in modules...

(https://devblogs.microsoft.com/typescript/announcing-typescript-3-4/#type-checking-for-globalthis)

It shows an example how typed variable may be declared in the global scope. But it doesn't cover scenario when one needs to declare variable/function/namespace which use a type/interface declared in some module.

I think that the documentation should clearly explain that it's not possible or show an example how to do it (if it's possible).

Use Case

One creates a function with input parameter and/or return type declared in module.
She wants to make this function available on global.

It's an anti-pattern in the same way as augmenting types from external modules, but as discussed here #5292 it exists in real life.

One may have a well organised non-poluting-global modules-based codebase. But at some point by some reason she have to provide an interface for a global consumer. Moving all exposed interfaces and types to global and rewrite the codebase which used to consume them from modules would be ugly and painful.

Example

import { SomeType, SomeInterface } from 'some/module';

function functionForGlobalConsumer(input: SomeType): SomeInterface {
  // some code...
}

globalThis.functionForGlobalConsumer = functionForGlobalConsumer;

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

Inizia dall’esempio della documentazione di TypeScript 3.4 globalThis collegato nell’issue e riproduci lo scenario mostrato con un tipo importato. Determina se la documentazione debba descrivere un pattern supportato o una limitazione, quindi aggiorna le indicazioni pertinenti sull’ambito globale con un esempio o una spiegazione chiari e verifica che copra il caso d’uso indicato.

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

Valutazione

Stack tecnologico
typescript
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.