Description
Suggestion
π Search Terms
webworker, dom, node, isomorphic
β Viability 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
β Suggestion
Sometimes code is designed to run in multiple environments. For example, code may run in both nodejs and the browser, or a web page and a web worker.
Currently, you can use tsconfig to say your environment is both a 'DOM' and 'webworker', but that's never true in the wild. It's 'DOM' or 'webworker'.
Take this example (playground link):
localStorage.set('foo', 'bar');
importScripts('hello.js');
TypeScript is fine with this if you include both 'DOM' and 'webworker' libs, but this code will fail in a webworker, because localStorage
doesn't exist, and it will fail in a page, because importScripts
doesn't exist.
This would be solved by a feature that allows developers to specify the environment as A or B. The code above would show errors, because localStoage
and importScripts
may not exist.
π Motivating Example
Hopefully the above description provides this.
π» Use Cases
Hopefully the above description provides this.