1. Namespaces can be merged
namespace Todo {
export let id = 2342
}
namespace Todo {
export let title = 'todo list';
}
console.log(Todo.id, Todo.title);
// Todo.id, Todo.title can be visited.
Can not set the same name of the property.
namespace Todo {
export let id = 2342; // Error: Cannot redeclare block-scoped variable 'id'.ts(2451)
}
namespace Todo {
export let title = 'todo list';
export let id = 456; // Error: Cannot redeclare block-scoped variable 'id'.ts(2451)
}
2. Merged with interface and Class
interface Todo {
content: string
}
class Todo {
comment() {
}
}
namespace Todo {
export let title = 'todo list';
}
const todo: Todo = {
content: 'todo content',
comment() {
}
}
The properties in Namespace is the Static properties.
class Todo {
comment() {
}
static title = 'meeting' // error: Duplicate identifier 'title'.ts(2300)
}
namespace Todo {
export let title = 'todo list'; // error: Duplicate identifier 'title'.ts(2300)
}
Can not be located prior to a Class or Function
namespace Todo { // error: A namespace declaration cannot be located prior to a class or function with which it is merged.ts(2434)
export let title = 'todo list';
}
class Todo {
comment() {
}
}
0 Comments