Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Recently I've been studying ES6, and that lead to me using Babel a lot. Being the curious type, I started looking at the Babel Github repository to know how they built this awesome tool, and know if I can somehow contribute.

However, I came across this file , and it has things like declare class BabelNodeSourceLocation {} written all over it, and the file ends with .js.

This got me very confused, and I am now wondering whether there's a declare keyword in JavaScript that I didn't know of, or is this just a Babel-specific syntax? All my Google searches resulted in nothing.

Update: Putting the code in the Babel REPL resulted in nothing. Babel just ignored the code and did not produce any equivalent ES5 output. It also did not throw any error.

This becomes confusing with import statements used to pull in a declared module. flow-typed makes type imports available by importing the types by the same string as the npm module name. Magically, flow seems to now then that these import statements refer to the content in the flow-typed directory, and not the module itself. But how webpack, babel, and something like eslint treat this is quite unclear. this has come up for me in trying to create custom flow types and make them importable. See here fraxture May 24, 2018 at 14:19 Note that there is also a declare keyword in Typescript: stackoverflow.com/questions/43335962/… Hawkeye Parker Feb 9 at 19:03

With Flow, you can declare a global class that allows you to reference the class type anywhere in your project. This has no affect on runtime code and won't affect babel output.

An example from the docs :

declare class URL {
  constructor(urlStr: string): URL;
  toString(): string;
  static compare(url1: URL, url2: URL): boolean;

And then in your project you can reference URL as a class type.

Similarly, you can declare other global Types, Modules, Functions, Variables. A good way to keep them organized.

Note that there is also a declare keyword in Typescript: stackoverflow.com/questions/43335962/… – Hawkeye Parker Feb 9 at 19:03

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.