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
declare module "*!text" {
const content: string;
export default content;
}
// Some do it the other way around.
declare module "json!*" {
const value: any;
export default value;
}
Now you can import things that match "!text"
or "json!
"
.
import fileContent from "./xyz.txt!text";
import data from "json!http://example.com/data.json";
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
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.
import { readFile } from "myLibrary/fileSystem/readFile";
readFile(); // readFile is 'any'