microsoft / microsoft/TypeScript
Making isServer() and isClient() checks work in TypeScript
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Search Terms
React, Isomorphic, Client/Server, Frontend, Backend, process, server, window
Suggestion
Add a typing mechanism for narrowing down global variable declarations.
function isServer(): globalThis is typeof globalThis & NodeJS.Global;
function isClient(): globalThis is typeof globalThis & Window;
Use Cases
Isomorphic JavaScript code that runs in browser and server often checks for environment to decide to do various things. Today we have to add both dom.d.ts and @types/node to bring in all global variables from both declarations such as window and process in.
In runtime however it's not safe to assume window exists so we have to check for it:
Examples
if (isClient()) {
window.location.search = '?foo'
}
if (isServer()) {
console.log(process.env.FOO);
}
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
L'issue non indica file del repository, test o punti di ingresso. Inizia tracciando il narrowing del flusso di controllo di TypeScript e le dichiarazioni di tipo globali, quindi esamina i test esistenti per i predicati di tipo definiti dall'utente. L'attività è completata quando è definito un meccanismo di narrowing specificato e accettato per isServer() e isClient(), con test che coprono gli esempi del browser e del server.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, 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
- 35/100