Issue
I was reading about the Node.js Assertion Class when I was blown back from what I saw -- there was an as
operator inside of an import
statements destructor-brackets! This surprised for couple of reasons:
- For one, I had thought, previously, that the keyword
as
was a TypeScript reserved word only, and had nothing to do with JavaScript or Node.js, and I am not alone on this. I DuckDuckGo'd theas
keyword before authoring this question and got the following StackOverflow question as one of the search results:
Everyone who answered the question above all stated the same thing, "The keyword as
is a TypeScript thing". It seems that as
being part of Node.js is not very well known yet.
- The other reason I was surprised, which leads into my question, had to do with where the
as
keyword was located, inside of animport
statements destructor-brackets, as seen below:
(If you haven't viewed the Node.js link above, you can skip it, below is the use of as
that I keep referring to.)
import { strict as assert } from 'assert';
import assert from 'assert/strict';
So what I would like to know is how does the as
keyword work in Node.JS? Is it going to have use cases outside of assertions, or is the import statement above a singular use-case?
Solution
as
keyword is a part of ES6 module syntax.
You use as
when you need to rename the exported named variables/functions in order to avoid conflict between library and your code, or just because you want to rename it XD
You can read more about it here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#import_an_export_with_a_more_convenient_alias
Answered By - moonlight8978 Answer Checked By - Robin (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.