ts之 命名空间 namespace
ts之 命名空间 namespace
namespace A {
export interface AAA {
name: string;
}
export const arr: AAA[] = [{ name: "ppp" }];
export namespace B {
export const bbb = "bbb";
}
}
console.log("arr", A.arr);
import BBB = A.B;
ts之 三斜线指令 ( 引入其他.ts文件 )
- 或者安装 声明文件
npm i @types/node -D
app.ts
namespace AAA {
export const bbb = "bb";
}
index.ts
ts之 声明文件 d.ts - declare
01:declare声明express第三方模块
typings 为代码或者第三方模块 编写声明文件
declare module "express" {
interface Router {
get(path: string, cb?: (req: any, res: any) => void): void;
}
interface App {
use(path: string, router: any): void;
listen(port: number, cb?: () => void);
}
interface Express {
(): App;
Router(): Router;
}
const express: Express;
export default express;
}
declare var a: boolean;
declare var add = (a: number, b: number) => number;
index.ts
import express from "express";
const app = express();
const router = express.Router();
app.use("./api", router);
router.get("/api", (req, res) => {
res.json({
code: 200,
});
});
app.listen(9001, () => {
console.log("服务器已起 端口为9001");
});
a = true;
add = (a: number, b: number) => {
return a + b;
};
add(1, 2);
02:declare声明 变量和函数
typings / app.d.ts
declare var b: boolean;
declare var add = (a: number, b: number) => number;
declare var sub = (a: number, b: number) => number;
app.ts
b = true;
add = (a: number, b: number) => {
return a + b;
};
add(1, 2);
sub = (a: number, b: number) => {
return a - b;
};
sub(1, 2);
index.ts