Use Externs While Doing ES6 Imports

87 views
Skip to first unread message

zad0m

unread,
Apr 19, 2019, 10:54:17 PM4/19/19
to Closure Compiler Discuss
Hi! Thanks you for the latest release. I was wondering, how can I use externs for a package that I import using named exports? For example, say I want to do 

import { render, h } from 'preact'

Here the compiler would have to infer that the type comes from the Preact extern, because I didn't import it like 

import preact from 'preact'
preact
.render(preact.h('div', null, 'Hello World'))

wouldn't it? Is there a better way to do it such as annotations? But how can I annotate cast import from? How does the inference algorithm works? Thanks in advance for the answers.

Best regards. 

zad0m

unread,
Apr 29, 2019, 6:35:54 AM4/29/19
to Closure Compiler Discuss
This question doesn't make sense actually, when imports are used there are no externs since the code is directly taken from the imported library. Not sure why I confused the two. thx 

dpapad

unread,
Apr 8, 2022, 10:13:08 AM4/8/22
to Closure Compiler Discuss
> This question doesn't make sense actually, when imports are used there are no externs since the code is directly taken from the imported library.

Actually the question still makes sense. Imagine that the imported library is written in TypeScript, and it is imported via ES6 by a JS file. Getting the JS file to pass type checking with Closure Compiler would require being able to provide Closure externs for the imported library, which apparently is still not supported (3 years after last message in this thread).

Bradford Smith

unread,
Apr 8, 2022, 2:11:11 PM4/8/22
to Closure Compiler Discuss
Our use of closure-compiler in Google is nearly 100% based on using `goog.module()` rather than ES6 imports.
We use tsickle to convert TS code into JS suitable for compilation with closure-compiler, and it also generates files that use `goog.module()`.

This is why there isn't much OSS support for ES modules with closure-compiler.

For the case you describe, it is possible to write hand-crafted externs files, but I'm not aware of any automated way to generate them.

Your import statement might end up looking something like this.

<code>
import { /** @type {!Preact.render} */ render,  /** @type {!Preact.h} */ h } from 'preact';
</code>

Sorry,
Bradford
Reply all
Reply to author
Forward
0 new messages