Exploring Documentation
The code snippet declare module '*.yaml' {...}
introduces an ambient module declaration with the specific name *.yaml
. This naming convention utilizes a wildcard character "*" in TypeScript version 2.0 to streamline module declarations for extensions.
In TypeScript 2.0, the introduction of the "*" wildcard allows for consolidated module declarations under a common extension, reducing the need for repetitive individual declarations.
Referencing TypeScript's module resolution logic documentation:
The compiler searches for imported modules by following either the Classic or Node strategy based on the specified moduleA location.
If the search is unsuccessful and the module name is non-relative (as is the case for "moduleA"), the compiler looks for an ambient module declaration as a fallback. Non-relative imports are elaborated further in the subsequent section.
Application in Real Context
An intriguing fact emerges later on:
Relative imports are resolved in relation to the importing file and cannot target an ambient module declaration directly. Use relative imports only for strictly local modules that retain their path integrity at runtime.
This revelation prompts curiosity about how the compiler resolves import paths like
import singleFile from "../../texts/myFile.yaml"
, which seemingly combines relative addressing with an ambient module declaration.
Contrasting Documentation and Execution
The pivotal reason behind successful resolution lies in the lack of path sensitivity in wildcard application. The presence of "../" indicating "relative parent" within the filesystem structure does not deter the matching process between imported file paths and ambient module names.
To test this concept:
import singleFile from "../../../../AAA/texts/myFile.yaml"
import singleFile from "/texts/myFile.yaml"
import singleFile from "C:\\//\\/myFile.yaml"
Despite potential filesystem obstacles, these examples resolve to the respective ambient module declarations successfully.
Final Insight
If attempts such as
import allFiles from "../../texts"
fail, it stems from the absence of wildcard inclusion in the ambient module declaration name for the "text" folder. Updating the declaration name to incorporate a wildcard or adjusting the import statement can rectify this issue. Considering the utilization of WebPack for module resolution, strategizing the approach ensures cohesive module handling without unintended interferences.
Reflective Closure
An inconsistency appears between current documentation statements and practical outcomes:
When an import is non-relative, the compiler resorts to seeking an ambient module declaration.
[...]
A relative import aligns its resolution with the importing file and does not lead to direct ambient module linkage. This distinction emphasizes using relative imports judiciously for consistent runtime positioning.
While my interpretation may not align perfectly with the TypeScript compiler behavior, a reevaluation of the documentation could enhance clarity regarding wildcard functionalities. I have initiated an issue ticket (#2559) on the TS Website repository to address this discrepancy.