0

    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:

  1. 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 the as 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.

  1. The other reason I was surprised, which leads into my question, had to do with where the as keyword was located, inside of an import 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?

JΛYDΞV
  • 8,532
  • 3
  • 51
  • 77

1 Answers1

1

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

moonlight8978
  • 156
  • 1
  • 3
  • So its a JavaScript thing then? I knew it was used to rename, but I thought it was only used for testing, and I was under the impression that it was only available in typescript. Anywho, now I know... – JΛYDΞV May 11 '21 at 06:22