TypeScript Version: Version 2.0.2-rc
Code
var url: URL = null;
compiled with:
$ tsc --version
Version 2.0.2
$ tsc --lib es6 app.ts
Expected behavior:
Successful compilation (to var url = null;)
Actual behavior:
app.ts(2,10): error TS2304: Cannot find name 'URL'.
even though URL is defined in lib.es6.d.ts
Url is defined in the Dom. Use --lib es6,dom
Ok, thanks.
How are we to discover these? Are these specifics documented? Is there a place to look it up in the code?
How are these delineations decided?
URL is in DOM because URL isn't defined as a global object by the ES spec. See http://www.ecma-international.org/ecma-262/5.1/#sec-15
A quick way to check is to see if the object is in nodejs's runtime env:
D:\github>node
> URL
ReferenceError: URL is not defined
at repl:1:1
if you are doing web development you should include the dom definitions. if you are doing node development you should not. similarly if you are running in a webworker you should include webworker.
Well, I'm referring to Electron's main worker's environment which I guess is a bit of a hybrid. Session.resolveProxy takes a URL parameter. I don't see where it is defined differently than the implied es6 (and used in the current github-electron.d.ts) but nor do I know that it isn't just a bit of an error in the docs which was then reflected in the definitions (instead of string).
class Session extends EventEmitter {
...
resolveProxy(url: URL, callback: (proxy: string) => void): void;
...
}
The reason it matters is that I'm preparing a separated github-electron-main and github-election-renderer to more accurately match both environments rather than the union of both environments. The github-electron-main is just es6,webworker except that URL is missing, and the other stuff in dom isn't present.
It's the only one, and pretty easily resolved (as I have done) by redefining URL. I asked this before I figured that out.
I do not know much about electron to be of help here. others on the thread might have more information.
❯ node --version
v7.10.0
❯ node
> require('url').URL
[Function: URL]
URL is defined in Node, and it follows the WHATWG spec.
How do we typehint against it without using --lib dom?
It appears if we simply add,
import {URL} from 'url';
Where-ever we use it, then tsc won't complain and if you don't actually use it for anything other than typehints, it won't be included in the output. Nice!
Is there a workaround for @type/ package definitions that use it if I am in node and want to use? (E.g. I'm using got.js -- works fine in node, just its types don't) Should I open an issue with the @type maintainer? ... or is there some to tell the compiler to find it in the 'url' package?
Most helpful comment
URL is in DOM because URL isn't defined as a global object by the ES spec. See http://www.ecma-international.org/ecma-262/5.1/#sec-15
A quick way to check is to see if the object is in nodejs's runtime env: