Home >Web Front-end >JS Tutorial >Interface in Vanilla JavaScript with VS Code IntelliSense
TL;DR;
Pure JavaScript interface simulation, using the code analysis function of VS Code IntelliSense, can be called a skill. Through the clever combination of object factory and empty function, interface-like code prompts and type checking are implemented, and the null value merging operator (??) is used to simplify the code. In a production environment, build scripts need to be used to remove unnecessary interface code.
The following is an example of a pure JavaScript interface that relies on code analysis in a code editor like VS Code IntelliSense, so it can also be called a trick:
<code class="language-javascript">var interface = () => null; var InterfaceOptions = () => ({ name: '', }); InterfaceOptions = interface; // 使用示例 // ===== let opt = InterfaceOptions`` ?? { name: 'Bagel', }; function createItem(options = InterfaceOptions``) { // ... } createItem(opt);</code>
Here is an example of renaming properties in pure JavaScript:
You create an object factory that initializes the property's code analysis and then replaces the object with a function that returns null. This enables some declaration tricks using the null coalescing operator (??) to keep your code tidy.
It works with arrays too! See the sample code in the Trivia #4 section below.
createBox()
option. InterfaceBoxOptions
or something like that, ok! boxOptions
return null. That’s it, I got a working interface-like setup in pure JavaScript. Probably should have used TypeScript from the start, but I'm in the wild west.
For the object declaration, I wrote a build script that replaced interfaceName ??
with an empty string before passing it to Terser, since the compressor won't judge the null value returned by the merge.
Before:
<code class="language-javascript">var interface = () => null; var InterfaceOptions = () => ({ name: '', }); InterfaceOptions = interface; // 使用示例 // ===== let opt = InterfaceOptions`` ?? { name: 'Bagel', }; function createItem(options = InterfaceOptions``) { // ... } createItem(opt);</code>After
:
<code class="language-javascript">let opt = InterfaceOptions`` ?? { name: null, };</code>
If you don’t delete the interface part, the compressed code may look like this:
<code class="language-javascript">let opt = { name: null, };</code>
var
For interfaces, you should use var
instead of let
or const
. This ensures that it is removed when using Terser to compress at the top level.
<code class="language-javascript">let opt = (() => null)() ?? { name: null, };</code>
<code class="language-javascript">var interface = () => null; var InterfaceOptions = () => ({ name: null, }); InterfaceOptions = interface;</code>
Terser issue #572: Remove variables that are only assigned but never read.
If global interface functions are not available, for example if you are writing a library for someone else, you can do this:
<code class="language-javascript">// terser 选项 { toplevel: true, compress: true, // ... }</code>
If you haven’t figured it out yet, here’s how:
<code class="language-javascript">var interface = () => null; var InterfaceOptions = () => ({ name: '', }); InterfaceOptions = interface; // 使用示例 // ===== let opt = InterfaceOptions`` ?? { name: 'Bagel', }; function createItem(options = InterfaceOptions``) { // ... } createItem(opt);</code>
Not bad, right?
Yes, but you need to create a separate interface for the array for IntelliSense to work properly. I would say it's pretty confusing.
Example 1:
<code class="language-javascript">let opt = InterfaceOptions`` ?? { name: null, };</code>
But it does have benefits. Now you know what to add to the array!
Example 2:
<code class="language-javascript">let opt = { name: null, };</code>
Like this? No, code analysis breaks for this specific object.
But you can do this:
<code class="language-javascript">let opt = (() => null)() ?? { name: null, };</code>
All images have been preserved and used the same format as the original text. Since the image URL cannot be processed directly, I retained the /uploads/...
path in the original text. Please make sure these paths are correct for your environment.
The above is the detailed content of Interface in Vanilla JavaScript with VS Code IntelliSense. For more information, please follow other related articles on the PHP Chinese website!