// 文件名 SomeInterface.ts
export interface SomeInterface {
代码部分
}
import someInterfaceRef = require("./SomeInterface")
// 注意: 导入导出的名字,可以不一致
export default XXX
import xxxx from "路径"
// 注意点: 导入导出的名字必须一致
export XXX
import {XXX} from "路径"
exports.xxx = xxx
const xxx = require("path")
const {XX,XX} = require("path")
module.exports.xxx = xxx
const xxx = require("path")
const {xx,xx} = require("path")
export interface IPerson{
name: string
age: number
sex: boolean
show(): void
}
export const obj = {
name: "富甲一方钱七"
}
import Test = require("./mpduleTest")
class UserInfo implements Test.IPerson{
name = "法外狂徒张三"
age = 18
sex = true
show() {
console.log('法外狂徒')
}
}
let person = new UserInfo()
console.log(person.name)
import {obj} from "./mpduleTest"
console.log(obj.name)
// ./namespace.ts
export namespace D{
export const d = 230;
}
namespace A {
export const a = 10
}
console.log(A.a)
// 嵌套命名空间
namespace B{
export const b = 200;
export namespace C{
export const c = 12
}
}
console.log(B.b) // 200
console.log(B.C.c) // 12
// 简化命名空间
import c = B.C.c
console.log(c) // 12
import {D} from "./namespaceTest"
console.log(D.d) //230
///
// namespaceTest2.ts
namespace User{
export interface IName {
uname: string
}
}
///
const username: User.IName = {
uname: '法外狂徒张三'
}
// 接口
interface ITest {
name: string
}
interface ITest {
age: number
}
class Per implements ITest {
name: string = '法外狂徒张三'
age: number = 18
}
let per = new Per()
console.log(per.name,per.age)
interface ITest {
show(value: number): number
}
interface ITest {
show(value: string): number
}
const func: ITest = {
show(value: any): number {
if(typeof value == "string"){
return value.length
}else{
// toFixed() 四舍五入
return value.toFixed()
}
}
}
// 命名空间
namespace ITest {
export const num = 10
}
namespace ITest {
// num error,同名的命名空间不能有同名的变量\方法等
// export const num = 100
}
// 命名空间与类的合并
class Perso {
// prototypes上面
say(): void{
console.log("say hello")
}
}
namespace Perso {
export const hi = (): void =>{
console.log("say Hi")
}
}
console.dir(Perso)
// 命名空间与函数的合并
// 注意点: 函数里面可以使用命名空间定义的变量
function getCounters(){
getCounters.count++;
console.log(getCounters.count)// 1
}
namespace getCounters {
export let count: number = 0
}
getCounter()
// 命名空间和枚举合并
enum Gender{
Male,
Female
}
namespace Gender{
export const money: number = 18
}
console.log(Gender) // {'0': 'Male','1': 'Female',Male:0,Female: 1,money: 18}