microsoft / microsoft/TypeScript
Making isServer() and isClient() checks work in TypeScript
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Das Issue nennt keine Dateien im Repository, Tests oder Einstiegspunkte. Beginne damit, TypeScript's Control-Flow-Narrowing und globale Typdeklarationen nachzuverfolgen, und prüfe anschließend vorhandene Tests für benutzerdefinierte Typprädikate. Die Aufgabe ist erledigt, wenn ein spezifizierter, akzeptierter Narrowing-Mechanismus für isServer() und isClient() feststeht und Tests die Browser- und Serverbeispiele abdecken.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100