Attempting to re-export modules according to the TypeScript language website - using SystemJS as the module loader:
app.ts
import * as s from "./AllValidators";
// Some samples to try
let strings = ["Hello", "98052", "101"];
// Validators to use
let validators: { [s: string]:s.StringValidator; } = {};
validators["ZIP code"] = new s.ZipCodeValidator();
validators["Letters only"] = new s.LettersOnlyValidator();
// Show whether each string passed each validator
strings.forEach(s => {
for (let name in validators) {
console.log(`"${s}" - ${validators[name].isAcceptable(s) ? "matches" : "does not match"} ${name}`);
}
});
AllValidators.ts
export * from "./Validation";
export * from "./LettersOnlyValidator";
export * from "./ZipCodeValidator";
LettersOnlyValidator.ts
import { StringValidator } from "./Validation";
const lettersRegexp = /^[A-Za-z]+$/;
export class LettersOnlyValidator implements StringValidator {
isAcceptable(s: string) {
return lettersRegexp.test(s);
}
}
Validation.ts
export interface StringValidator {
isAcceptable(s: string): boolean;
}
ZipCodeValidator.ts
import { StringValidator } from "./Validation";
const numberRegexp = /^[0-9]+$/;
export class ZipCodeValidator implements StringValidator {
isAcceptable(s: string) {
return s.length === 5 && numberRegexp.test(s);
}
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>TypeScript HTML App</title>
<script src="system.js"></script>
<script src="typescript.js"></script>
<script>
System.config({
transpiler: 'typescript',
packages: {
src: {
defaultExtension: 'ts'
}
}
});
System
.import('src/app')
.then(null, console.error.bind(console));
</script>
</head>
<body>
<h1>TypeScript HTML App</h1>
<div id="content"></div>
</body>
</html>
When running in the browser (Chrome), there is an error in the console log: