😇Tìm hiểu namspace, mở rộng namspace (ok)
Chú ý 🤣
Nếu bạn dùng namespace trong một file có khai báo import hoặc export ở cấp cao nhất, file đó sẽ trở thành một module, và namespace sẽ không còn thuộc phạm vi toàn cục nữa. Trong trường hợp này, bạn cần export namespace để sử dụng nó ở nơi khác.
ts-node
không hỗ trợ trực tiếp/// <reference path="..." />
, nên bạn phải biên dịch bằngtsc
trước khi chạy bằngnode
. xem Example 2.2Dùng Cách 1 (
export/import
) nếu bạn muốn code dễ chạy trực tiếp bằngts-node
.Dùng Cách 2 (
/// <reference path="..." />
) nếu bạn muốn code theo kiểu namespace truyền thống mà không dùng module.Dùng Cách 3 Cấu hình tsconfig.json để không dùng
reference nữa một câu hỏi đặt ra liệu khi khia báo bằng tsconfig.json rồi liệu có chạy được trược tiếp ts-node không? câu trả lời là không nhé bắt buộc phải biên dịch bằng tsc rồi sau đó chạy node đó là hạn chế của ts-node

Example 1

Trong TypeScript, bạn có thể mở rộng một namespace (không gian tên) bằng cách khai báo lại namespace đó và thêm các thành phần mới vào nó. Dưới đây là một ví dụ cụ thể:
Example 2.1 (Ví dụ Mở rộng namespace trong typescript 2 file khác nhau)
Đây có lẽ là ví dụ đúng nhất, khi chạy trực tiếp ts-node main3.ts nó báo lỗi như này 😂 không biết có phải do cấu hình file tsconfig.json sai không? hay cần khai báo thêm d.ts nữa

namespaceA.ts
namespace MyNamespace {
export const name = "TypeScript";
}
namespaceB.ts
/// <reference path="namespaceA.ts" />
namespace MyNamespace {
export const version = "5.0";
export function getInfo() {
return `${name} - Version ${version}`;
}
}
main3.ts
/// <reference path="namespaceA.ts" />
/// <reference path="namespaceB.ts" />
console.log(MyNamespace.getInfo()); // Output: "TypeScript - Version 5.0"

Exampl 2.2 (Cấu hình tsconfig.json để không sử dụng reference nữa)
tsconfig.json
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"outDir": "./dist",
"strict": true
},
"include": ["**/*"],
"exclude": ["node_modules", "dist"]
}

namespaceA.ts
namespace MyNamespace {
export const name = "TypeScript";
}
namespaceB.ts
namespace MyNamespace {
export const version = "5.0";
export function getInfo() {
return `${name} - Version ${version}`;
}
}
main3.ts
console.log(MyNamespace.getInfo()); // Output: "TypeScript - Version 5.0"
Đọc thêm giải thích ở mục con sẽ giải thích sử dụng include, exclude, files
Example xx

namespace1.ts
import { Shared } from './shared1';
namespace Namespace1 {
export function greet(name: string) {
Shared.sayHello(name);
}
}
Namespace1.greet("Alice");
namespace2.ts
import { Shared } from './shared1';
namespace Namespace2 {
export function greet(name: string) {
Shared.sayHello(name);
}
}
Namespace2.greet("Bob");
shared1.ts
export namespace Shared {
export function sayHello(name: string) {
console.log(`Hello, ${name}!`);
}
}
main1.ts
import './namespace1';
import './namespace2';
tsconfig.json không cần dùng "include": ["*.ts"]
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": true,
"esModuleInterop": true
},
// "include": ["*.ts"]
}
Last updated