17

Given the example below.

import { QueryClientContract, TransactionClientContract } from '@ioc:Adonis/Lucid/Database'

The use of the colon has confused me and never understood where the imported files could be referenced from.

4
  • 6
    It's not part of JavaScript; it's something relevant for whatever module "bundler" you're using. JavaScript just says that the from value has to be a string constant; it says nothing about the semantics of the string contents.
    – Pointy
    Mar 6, 2021 at 15:55
  • And do you have idea of which bundler could be able to translate that? And what does that mean for the bundler?
    – myckhel
    Mar 6, 2021 at 16:04
  • not 100% familar with adonis.js, but I suspect it has something to do with scoped packages <docs.npmjs.com/about-scopes>. Some tooling might have installed the packages as scoped packages, and node (I am assuming node?) can resolve the package.
    – pandamakes
    Mar 6, 2021 at 16:08
  • I don't recognize that, sorry. Where did you see it?
    – Pointy
    Mar 6, 2021 at 16:08

3 Answers 3

1

Prefixes for modules have existed for a while, though until recently they didn't affect the module functionality at all (you can read more about this development here). It looks like this specific prefix on these modules was supplied by the installation process for Adonis.js and given that the toolkit uses custom commands for things like building production versions of your project, it may have some custom processes for e.g. loading minimised versions of a dependency specifically for production builds.

There is not currently a standardised meaning or effect of using a prefix like this, outside of node: (which restricts imports to core packages and prevents you from accidentally downloading a malicious package by mistake, due to a typo). Some projects use this for internal purposes and it will depend on the specific project what effects the prefix may or may not have.

0

In TypeScript, the : colon is used as a type annotation syntax to specify the type of a variable, parameter, or return value. However, in the context you provided, ':' is not specifically related to type annotations. It is used in the AdonisJS framework to define a declaration merging syntax.

In AdonisJS, the declaration declare module '@ioc:Adonis/Core/Route' is used to extend or augment the existing types or interfaces defined in the @ioc:Adonis/Core/Route module.

For example, you might have a custom route macro that you want to add to the existing Route module. By using the declare module syntax, you can add your custom methods or properties to the Route module, making them available for use throughout your application.

Here's an example of how you might use it:

declare module '@ioc:Adonis/Core/Route' {
  interface RouteContract {
    myCustomMethod: () => void;
  }
}

In the above code, we are declaring that the RouteContract interface in the @ioc:Adonis/Core/Route module should include a new method called myCustomMethod.

This syntax allows you to extend or modify the existing types or interfaces provided by AdonisJS or other libraries, enabling you to add your own custom functionality.

I hope this clarifies the usage of ':' in the AdonisJS declaration merging syntax. Let me know if you have any further questions!

-5

In import statement you are importing ambient module. I recommend you to read following documentation for more details.

https://www.typescriptlang.org/docs/handbook/modules.html#ambient-modules

1
  • 7
    Oooops! Maybe the referenced doc still made me understood nothing about it.
    – myckhel
    Mar 6, 2021 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.