Improve dom types for server side rendering environments #53971
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Suggestion
π Search Terms
DSG, SSG, SSR, Server Side Rendering, NextJS GatsbyJS React, DOM type definitions, environment tsconfig, lib.dom
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
I envision this as an option for
lib
:that would load any DOM apis optionally, as if window was typed as
Window | undefined
, which would require developers to include type checks when accessing those apis.Typescript might also be able to check for "use client" directives to narrow the type of window automatically.
π Motivating Example
Catch errors that could cause server side rendering to fail!
Somewhere in your react code, you may write:
π» Use Cases
As server side rendering is growing in popularity, web application code needs to safely run in both browser and node environments. Accidentally accessing DOM apis without checking them first is a common source of crashes in these types of apps. Debugging these issues is often more time consuming than it should be, and requires reading logs to determine what went wrong.
Currently, the best approach I've found is to use eslint-plugin-ssr-friendly to catch errors, but this sort of approach only catches surface level errors. (It's entirely based on scope. i.e. you can never use globals in some scopes even with type checks, and you can always use globals in other scopes even if they're dangerous).
The text was updated successfully, but these errors were encountered: