Wildcard character in module names

Importing none-code resources using module loaders extension (e.g. AMD or SystemJS) has not been easy before;previously an ambient module declaration had to be defined for each resource.

TypeScript 2.0 supports the use of the wildcard character (*) to declare a “family” of module names;this way, a declaration is only required once for an extension, and not for every resource.

Example

  1. declare module "*!text" {
  2. const content: string;
  3. export default content;
  4. }
  5. // Some do it the other way around.
  6. declare module "json!*" {
  7. const value: any;
  8. export default value;
  9. }

Now you can import things that match "!text" or "json!".

  1. import fileContent from "./xyz.txt!text";
  2. import data from "json!http://example.com/data.json";
  3. console.log(data, fileContent);

Wildcard module names can be even more useful when migrating from an un-typed code base.Combined with Shorthand ambient module declarations, a set of modules can be easily declared as any.

Example

  1. declare module "myLibrary/*";

All imports to any module under myLibrary would be considered to have the type any by the compiler;thus, shutting down any checking on the shapes or types of these modules.

  1. import { readFile } from "myLibrary/fileSystem/readFile";
  2. readFile(); // readFile is 'any'