模块
import JQuery from 'jquery'
export const a1: number = 1
export function fun ( ) {
console. log ( 'fun' )
}
declare let $: JQuery
export default $
import test2 from './test2.ts'
import { fun } from './test2.ts'
import * as test2All from './test2.ts'
命名空间
namespace Validation {
export interface StringValidator {
isAcceptable ( s: string) : boolean
}
const lettersRegexp = /^[A-Za-z]+$/
const numberRegexp = /^[0-9]+$/
export class LettersOnlyValidator implements StringValidator {
isAcceptable ( s: string) {
return lettersRegexp. test ( s)
}
}
export class ZipCodeValidator implements StringValidator {
isAcceptable ( s: string) {
return s. length === 5 && numberRegexp. test ( s)
}
}
}
const strings: string[ ] = [ "Hello" , "98052" , "101" ]
const validators: { [ s: string] : Validation. StringValidator } = { }
validators[ "ZIP code" ] = new Validation. ZipCodeValidator ( )
validators[ "Letters only" ] = new Validation. LettersOnlyValidator ( )
console. log ( 'ZIP code:' , validators[ "ZIP code" ] . isAcceptable ( strings[ 0 ] ) )
console. log ( 'Letters only:' , validators[ "Letters only" ] . isAcceptable ( strings[ 0 ] ) )