Javascript 如何将名称空间与TypeScript外部模块一起使用?

Javascript 如何将名称空间与TypeScript外部模块一起使用?,javascript,module,typescript,Javascript,Module,Typescript,我有一些代码: baseTypes.ts export namespace Living.Things { export class Animal { move() { /* ... */ } } export class Plant { photosynthesize() { /* ... */ } } } import b = require('./baseTypes'); export namespace Living.Things { // Err

我有一些代码:

baseTypes.ts

export namespace Living.Things {
  export class Animal {
    move() { /* ... */ }
  }
  export class Plant {
    photosynthesize() { /* ... */ }
  }
}
import b = require('./baseTypes');

export namespace Living.Things {
  // Error, can't find name 'Animal', ??
  export class Dog extends Animal {
    woof() { }
  }
}
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');

namespace Living.Things {
  // Why do I have to write b.Living.Things.Plant instead of b.Plant??
  class Tree extends b.Living.Things.Plant {

  }
}
export namespace A {
    export class Twix { ... }
}
export namespace A {
    export class PeanutButterCup { ... }
}
export namespace A {
     export class KitKat { ... }
}
namespace A {
    export class Twix { ... }
}
namespace A {
    export class PeanutButterCup { ... }
}
namespace A {
     export class KitKat { ... }
}
export class Twix { ... }
export class PeanutButterCup { ... }
export class KitKat { ... }
export default class SomeType {
  constructor() { ... }
}
function getThing() { return 'thing'; }
export default getThing;
export class SomeType { ... }
export function someFunc() { ... }
export namespace Animals {
  export class Dog { ... }
  export class Cat { ... }
}
export namespace Plants {
  export class Tree { ... }
}
export class Animal {
    move() { /* ... */ }
}

export class Plant {
    photosynthesize() { /* ... */ }
}
import b = require('./baseTypes');

export class Dog extends b.Animal {
    woof() { }
}   
import b = require('./baseTypes');

class Tree extends b.Plant {
}
import dog = require('./dog')
import tree = require('./tree')

export = {
    dog: dog,
    tree: tree
}
import LivingThings = require('./LivingThings');
console.log(LivingThings.Tree)
console.log(LivingThings.Dog)
export class Animal {
move() { /* ... */ }
}

export class Plant {
  photosynthesize() { /* ... */ }
}
import * as b from './base';

export class Dog extends b.Animal {
   woof() { }
} 
import { Dog } from './dog'

namespace things {
  export const dog = Dog;
}

export = things;
import * as things from './things';

console.log(things.dog);
import b = require('./baseTypes');

export module Living.Things {
    // Error, can't find name 'Animal', ??
    // Solved: can find, if properly referenced; exporting modules is useless, anyhow
    export class Dog extends b.Living.Things.Animal {
        public woof(): void {
            return;
        }
    }
}
// Error, can't use the same name twice, ??
// Solved: cannot declare let or const variable twice in same scope either: just use a different name
import b = require('./baseTypes');
import d = require('./dog');

module Living.Things {
    // Why do I have to write b.Living.Things.Plant instead of b.Plant??
    class Tree extends b.Living.Things.Plant {
    }
}
export namespace Bookname{
export class Snows{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
export class Adventure{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
}





export namespace TreeList{
export class MangoTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
export class GuvavaTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
}

dog.ts

export namespace Living.Things {
  export class Animal {
    move() { /* ... */ }
  }
  export class Plant {
    photosynthesize() { /* ... */ }
  }
}
import b = require('./baseTypes');

export namespace Living.Things {
  // Error, can't find name 'Animal', ??
  export class Dog extends Animal {
    woof() { }
  }
}
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');

namespace Living.Things {
  // Why do I have to write b.Living.Things.Plant instead of b.Plant??
  class Tree extends b.Living.Things.Plant {

  }
}
export namespace A {
    export class Twix { ... }
}
export namespace A {
    export class PeanutButterCup { ... }
}
export namespace A {
     export class KitKat { ... }
}
namespace A {
    export class Twix { ... }
}
namespace A {
    export class PeanutButterCup { ... }
}
namespace A {
     export class KitKat { ... }
}
export class Twix { ... }
export class PeanutButterCup { ... }
export class KitKat { ... }
export default class SomeType {
  constructor() { ... }
}
function getThing() { return 'thing'; }
export default getThing;
export class SomeType { ... }
export function someFunc() { ... }
export namespace Animals {
  export class Dog { ... }
  export class Cat { ... }
}
export namespace Plants {
  export class Tree { ... }
}
export class Animal {
    move() { /* ... */ }
}

export class Plant {
    photosynthesize() { /* ... */ }
}
import b = require('./baseTypes');

export class Dog extends b.Animal {
    woof() { }
}   
import b = require('./baseTypes');

class Tree extends b.Plant {
}
import dog = require('./dog')
import tree = require('./tree')

export = {
    dog: dog,
    tree: tree
}
import LivingThings = require('./LivingThings');
console.log(LivingThings.Tree)
console.log(LivingThings.Dog)
export class Animal {
move() { /* ... */ }
}

export class Plant {
  photosynthesize() { /* ... */ }
}
import * as b from './base';

export class Dog extends b.Animal {
   woof() { }
} 
import { Dog } from './dog'

namespace things {
  export const dog = Dog;
}

export = things;
import * as things from './things';

console.log(things.dog);
import b = require('./baseTypes');

export module Living.Things {
    // Error, can't find name 'Animal', ??
    // Solved: can find, if properly referenced; exporting modules is useless, anyhow
    export class Dog extends b.Living.Things.Animal {
        public woof(): void {
            return;
        }
    }
}
// Error, can't use the same name twice, ??
// Solved: cannot declare let or const variable twice in same scope either: just use a different name
import b = require('./baseTypes');
import d = require('./dog');

module Living.Things {
    // Why do I have to write b.Living.Things.Plant instead of b.Plant??
    class Tree extends b.Living.Things.Plant {
    }
}
export namespace Bookname{
export class Snows{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
export class Adventure{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
}





export namespace TreeList{
export class MangoTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
export class GuvavaTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
}
tree.ts

export namespace Living.Things {
  export class Animal {
    move() { /* ... */ }
  }
  export class Plant {
    photosynthesize() { /* ... */ }
  }
}
import b = require('./baseTypes');

export namespace Living.Things {
  // Error, can't find name 'Animal', ??
  export class Dog extends Animal {
    woof() { }
  }
}
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');

namespace Living.Things {
  // Why do I have to write b.Living.Things.Plant instead of b.Plant??
  class Tree extends b.Living.Things.Plant {

  }
}
export namespace A {
    export class Twix { ... }
}
export namespace A {
    export class PeanutButterCup { ... }
}
export namespace A {
     export class KitKat { ... }
}
namespace A {
    export class Twix { ... }
}
namespace A {
    export class PeanutButterCup { ... }
}
namespace A {
     export class KitKat { ... }
}
export class Twix { ... }
export class PeanutButterCup { ... }
export class KitKat { ... }
export default class SomeType {
  constructor() { ... }
}
function getThing() { return 'thing'; }
export default getThing;
export class SomeType { ... }
export function someFunc() { ... }
export namespace Animals {
  export class Dog { ... }
  export class Cat { ... }
}
export namespace Plants {
  export class Tree { ... }
}
export class Animal {
    move() { /* ... */ }
}

export class Plant {
    photosynthesize() { /* ... */ }
}
import b = require('./baseTypes');

export class Dog extends b.Animal {
    woof() { }
}   
import b = require('./baseTypes');

class Tree extends b.Plant {
}
import dog = require('./dog')
import tree = require('./tree')

export = {
    dog: dog,
    tree: tree
}
import LivingThings = require('./LivingThings');
console.log(LivingThings.Tree)
console.log(LivingThings.Dog)
export class Animal {
move() { /* ... */ }
}

export class Plant {
  photosynthesize() { /* ... */ }
}
import * as b from './base';

export class Dog extends b.Animal {
   woof() { }
} 
import { Dog } from './dog'

namespace things {
  export const dog = Dog;
}

export = things;
import * as things from './things';

console.log(things.dog);
import b = require('./baseTypes');

export module Living.Things {
    // Error, can't find name 'Animal', ??
    // Solved: can find, if properly referenced; exporting modules is useless, anyhow
    export class Dog extends b.Living.Things.Animal {
        public woof(): void {
            return;
        }
    }
}
// Error, can't use the same name twice, ??
// Solved: cannot declare let or const variable twice in same scope either: just use a different name
import b = require('./baseTypes');
import d = require('./dog');

module Living.Things {
    // Why do I have to write b.Living.Things.Plant instead of b.Plant??
    class Tree extends b.Living.Things.Plant {
    }
}
export namespace Bookname{
export class Snows{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
export class Adventure{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
}





export namespace TreeList{
export class MangoTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
export class GuvavaTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
}
这一切都很令人困惑。我想让一堆外部模块都为同一名称空间贡献类型,
Living.Things
。这似乎根本不起作用——我在
dogs.ts
中看不到
Animal
。我必须在
tree.ts
中写入完整的名称空间名称
b.Living.Things.Plant
。跨文件在同一名称空间中组合多个对象是行不通的。我该怎么做?

糖果杯类比 版本1:每种糖果一杯 假设您编写了如下代码:

Mod1.ts

export namespace Living.Things {
  export class Animal {
    move() { /* ... */ }
  }
  export class Plant {
    photosynthesize() { /* ... */ }
  }
}
import b = require('./baseTypes');

export namespace Living.Things {
  // Error, can't find name 'Animal', ??
  export class Dog extends Animal {
    woof() { }
  }
}
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');

namespace Living.Things {
  // Why do I have to write b.Living.Things.Plant instead of b.Plant??
  class Tree extends b.Living.Things.Plant {

  }
}
export namespace A {
    export class Twix { ... }
}
export namespace A {
    export class PeanutButterCup { ... }
}
export namespace A {
     export class KitKat { ... }
}
namespace A {
    export class Twix { ... }
}
namespace A {
    export class PeanutButterCup { ... }
}
namespace A {
     export class KitKat { ... }
}
export class Twix { ... }
export class PeanutButterCup { ... }
export class KitKat { ... }
export default class SomeType {
  constructor() { ... }
}
function getThing() { return 'thing'; }
export default getThing;
export class SomeType { ... }
export function someFunc() { ... }
export namespace Animals {
  export class Dog { ... }
  export class Cat { ... }
}
export namespace Plants {
  export class Tree { ... }
}
export class Animal {
    move() { /* ... */ }
}

export class Plant {
    photosynthesize() { /* ... */ }
}
import b = require('./baseTypes');

export class Dog extends b.Animal {
    woof() { }
}   
import b = require('./baseTypes');

class Tree extends b.Plant {
}
import dog = require('./dog')
import tree = require('./tree')

export = {
    dog: dog,
    tree: tree
}
import LivingThings = require('./LivingThings');
console.log(LivingThings.Tree)
console.log(LivingThings.Dog)
export class Animal {
move() { /* ... */ }
}

export class Plant {
  photosynthesize() { /* ... */ }
}
import * as b from './base';

export class Dog extends b.Animal {
   woof() { }
} 
import { Dog } from './dog'

namespace things {
  export const dog = Dog;
}

export = things;
import * as things from './things';

console.log(things.dog);
import b = require('./baseTypes');

export module Living.Things {
    // Error, can't find name 'Animal', ??
    // Solved: can find, if properly referenced; exporting modules is useless, anyhow
    export class Dog extends b.Living.Things.Animal {
        public woof(): void {
            return;
        }
    }
}
// Error, can't use the same name twice, ??
// Solved: cannot declare let or const variable twice in same scope either: just use a different name
import b = require('./baseTypes');
import d = require('./dog');

module Living.Things {
    // Why do I have to write b.Living.Things.Plant instead of b.Plant??
    class Tree extends b.Living.Things.Plant {
    }
}
export namespace Bookname{
export class Snows{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
export class Adventure{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
}





export namespace TreeList{
export class MangoTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
export class GuvavaTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
}
Mod2.ts

export namespace Living.Things {
  export class Animal {
    move() { /* ... */ }
  }
  export class Plant {
    photosynthesize() { /* ... */ }
  }
}
import b = require('./baseTypes');

export namespace Living.Things {
  // Error, can't find name 'Animal', ??
  export class Dog extends Animal {
    woof() { }
  }
}
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');

namespace Living.Things {
  // Why do I have to write b.Living.Things.Plant instead of b.Plant??
  class Tree extends b.Living.Things.Plant {

  }
}
export namespace A {
    export class Twix { ... }
}
export namespace A {
    export class PeanutButterCup { ... }
}
export namespace A {
     export class KitKat { ... }
}
namespace A {
    export class Twix { ... }
}
namespace A {
    export class PeanutButterCup { ... }
}
namespace A {
     export class KitKat { ... }
}
export class Twix { ... }
export class PeanutButterCup { ... }
export class KitKat { ... }
export default class SomeType {
  constructor() { ... }
}
function getThing() { return 'thing'; }
export default getThing;
export class SomeType { ... }
export function someFunc() { ... }
export namespace Animals {
  export class Dog { ... }
  export class Cat { ... }
}
export namespace Plants {
  export class Tree { ... }
}
export class Animal {
    move() { /* ... */ }
}

export class Plant {
    photosynthesize() { /* ... */ }
}
import b = require('./baseTypes');

export class Dog extends b.Animal {
    woof() { }
}   
import b = require('./baseTypes');

class Tree extends b.Plant {
}
import dog = require('./dog')
import tree = require('./tree')

export = {
    dog: dog,
    tree: tree
}
import LivingThings = require('./LivingThings');
console.log(LivingThings.Tree)
console.log(LivingThings.Dog)
export class Animal {
move() { /* ... */ }
}

export class Plant {
  photosynthesize() { /* ... */ }
}
import * as b from './base';

export class Dog extends b.Animal {
   woof() { }
} 
import { Dog } from './dog'

namespace things {
  export const dog = Dog;
}

export = things;
import * as things from './things';

console.log(things.dog);
import b = require('./baseTypes');

export module Living.Things {
    // Error, can't find name 'Animal', ??
    // Solved: can find, if properly referenced; exporting modules is useless, anyhow
    export class Dog extends b.Living.Things.Animal {
        public woof(): void {
            return;
        }
    }
}
// Error, can't use the same name twice, ??
// Solved: cannot declare let or const variable twice in same scope either: just use a different name
import b = require('./baseTypes');
import d = require('./dog');

module Living.Things {
    // Why do I have to write b.Living.Things.Plant instead of b.Plant??
    class Tree extends b.Living.Things.Plant {
    }
}
export namespace Bookname{
export class Snows{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
export class Adventure{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
}





export namespace TreeList{
export class MangoTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
export class GuvavaTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
}
Mod3.ts

export namespace Living.Things {
  export class Animal {
    move() { /* ... */ }
  }
  export class Plant {
    photosynthesize() { /* ... */ }
  }
}
import b = require('./baseTypes');

export namespace Living.Things {
  // Error, can't find name 'Animal', ??
  export class Dog extends Animal {
    woof() { }
  }
}
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');

namespace Living.Things {
  // Why do I have to write b.Living.Things.Plant instead of b.Plant??
  class Tree extends b.Living.Things.Plant {

  }
}
export namespace A {
    export class Twix { ... }
}
export namespace A {
    export class PeanutButterCup { ... }
}
export namespace A {
     export class KitKat { ... }
}
namespace A {
    export class Twix { ... }
}
namespace A {
    export class PeanutButterCup { ... }
}
namespace A {
     export class KitKat { ... }
}
export class Twix { ... }
export class PeanutButterCup { ... }
export class KitKat { ... }
export default class SomeType {
  constructor() { ... }
}
function getThing() { return 'thing'; }
export default getThing;
export class SomeType { ... }
export function someFunc() { ... }
export namespace Animals {
  export class Dog { ... }
  export class Cat { ... }
}
export namespace Plants {
  export class Tree { ... }
}
export class Animal {
    move() { /* ... */ }
}

export class Plant {
    photosynthesize() { /* ... */ }
}
import b = require('./baseTypes');

export class Dog extends b.Animal {
    woof() { }
}   
import b = require('./baseTypes');

class Tree extends b.Plant {
}
import dog = require('./dog')
import tree = require('./tree')

export = {
    dog: dog,
    tree: tree
}
import LivingThings = require('./LivingThings');
console.log(LivingThings.Tree)
console.log(LivingThings.Dog)
export class Animal {
move() { /* ... */ }
}

export class Plant {
  photosynthesize() { /* ... */ }
}
import * as b from './base';

export class Dog extends b.Animal {
   woof() { }
} 
import { Dog } from './dog'

namespace things {
  export const dog = Dog;
}

export = things;
import * as things from './things';

console.log(things.dog);
import b = require('./baseTypes');

export module Living.Things {
    // Error, can't find name 'Animal', ??
    // Solved: can find, if properly referenced; exporting modules is useless, anyhow
    export class Dog extends b.Living.Things.Animal {
        public woof(): void {
            return;
        }
    }
}
// Error, can't use the same name twice, ??
// Solved: cannot declare let or const variable twice in same scope either: just use a different name
import b = require('./baseTypes');
import d = require('./dog');

module Living.Things {
    // Why do I have to write b.Living.Things.Plant instead of b.Plant??
    class Tree extends b.Living.Things.Plant {
    }
}
export namespace Bookname{
export class Snows{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
export class Adventure{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
}





export namespace TreeList{
export class MangoTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
export class GuvavaTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
}
您已创建此设置:

每个模块(一张纸)都有自己的杯子,名为
A
。这是没有用的-你实际上没有组织你的糖果在这里,你只是增加了一个额外的步骤(把它从杯子里拿出来)之间的你和对待


版本2:全球范围内的一个杯子 如果您没有使用模块,您可能会编写这样的代码(注意缺少
export
声明):

global1.ts

export namespace Living.Things {
  export class Animal {
    move() { /* ... */ }
  }
  export class Plant {
    photosynthesize() { /* ... */ }
  }
}
import b = require('./baseTypes');

export namespace Living.Things {
  // Error, can't find name 'Animal', ??
  export class Dog extends Animal {
    woof() { }
  }
}
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');

namespace Living.Things {
  // Why do I have to write b.Living.Things.Plant instead of b.Plant??
  class Tree extends b.Living.Things.Plant {

  }
}
export namespace A {
    export class Twix { ... }
}
export namespace A {
    export class PeanutButterCup { ... }
}
export namespace A {
     export class KitKat { ... }
}
namespace A {
    export class Twix { ... }
}
namespace A {
    export class PeanutButterCup { ... }
}
namespace A {
     export class KitKat { ... }
}
export class Twix { ... }
export class PeanutButterCup { ... }
export class KitKat { ... }
export default class SomeType {
  constructor() { ... }
}
function getThing() { return 'thing'; }
export default getThing;
export class SomeType { ... }
export function someFunc() { ... }
export namespace Animals {
  export class Dog { ... }
  export class Cat { ... }
}
export namespace Plants {
  export class Tree { ... }
}
export class Animal {
    move() { /* ... */ }
}

export class Plant {
    photosynthesize() { /* ... */ }
}
import b = require('./baseTypes');

export class Dog extends b.Animal {
    woof() { }
}   
import b = require('./baseTypes');

class Tree extends b.Plant {
}
import dog = require('./dog')
import tree = require('./tree')

export = {
    dog: dog,
    tree: tree
}
import LivingThings = require('./LivingThings');
console.log(LivingThings.Tree)
console.log(LivingThings.Dog)
export class Animal {
move() { /* ... */ }
}

export class Plant {
  photosynthesize() { /* ... */ }
}
import * as b from './base';

export class Dog extends b.Animal {
   woof() { }
} 
import { Dog } from './dog'

namespace things {
  export const dog = Dog;
}

export = things;
import * as things from './things';

console.log(things.dog);
import b = require('./baseTypes');

export module Living.Things {
    // Error, can't find name 'Animal', ??
    // Solved: can find, if properly referenced; exporting modules is useless, anyhow
    export class Dog extends b.Living.Things.Animal {
        public woof(): void {
            return;
        }
    }
}
// Error, can't use the same name twice, ??
// Solved: cannot declare let or const variable twice in same scope either: just use a different name
import b = require('./baseTypes');
import d = require('./dog');

module Living.Things {
    // Why do I have to write b.Living.Things.Plant instead of b.Plant??
    class Tree extends b.Living.Things.Plant {
    }
}
export namespace Bookname{
export class Snows{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
export class Adventure{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
}





export namespace TreeList{
export class MangoTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
export class GuvavaTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
}
global2.ts

export namespace Living.Things {
  export class Animal {
    move() { /* ... */ }
  }
  export class Plant {
    photosynthesize() { /* ... */ }
  }
}
import b = require('./baseTypes');

export namespace Living.Things {
  // Error, can't find name 'Animal', ??
  export class Dog extends Animal {
    woof() { }
  }
}
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');

namespace Living.Things {
  // Why do I have to write b.Living.Things.Plant instead of b.Plant??
  class Tree extends b.Living.Things.Plant {

  }
}
export namespace A {
    export class Twix { ... }
}
export namespace A {
    export class PeanutButterCup { ... }
}
export namespace A {
     export class KitKat { ... }
}
namespace A {
    export class Twix { ... }
}
namespace A {
    export class PeanutButterCup { ... }
}
namespace A {
     export class KitKat { ... }
}
export class Twix { ... }
export class PeanutButterCup { ... }
export class KitKat { ... }
export default class SomeType {
  constructor() { ... }
}
function getThing() { return 'thing'; }
export default getThing;
export class SomeType { ... }
export function someFunc() { ... }
export namespace Animals {
  export class Dog { ... }
  export class Cat { ... }
}
export namespace Plants {
  export class Tree { ... }
}
export class Animal {
    move() { /* ... */ }
}

export class Plant {
    photosynthesize() { /* ... */ }
}
import b = require('./baseTypes');

export class Dog extends b.Animal {
    woof() { }
}   
import b = require('./baseTypes');

class Tree extends b.Plant {
}
import dog = require('./dog')
import tree = require('./tree')

export = {
    dog: dog,
    tree: tree
}
import LivingThings = require('./LivingThings');
console.log(LivingThings.Tree)
console.log(LivingThings.Dog)
export class Animal {
move() { /* ... */ }
}

export class Plant {
  photosynthesize() { /* ... */ }
}
import * as b from './base';

export class Dog extends b.Animal {
   woof() { }
} 
import { Dog } from './dog'

namespace things {
  export const dog = Dog;
}

export = things;
import * as things from './things';

console.log(things.dog);
import b = require('./baseTypes');

export module Living.Things {
    // Error, can't find name 'Animal', ??
    // Solved: can find, if properly referenced; exporting modules is useless, anyhow
    export class Dog extends b.Living.Things.Animal {
        public woof(): void {
            return;
        }
    }
}
// Error, can't use the same name twice, ??
// Solved: cannot declare let or const variable twice in same scope either: just use a different name
import b = require('./baseTypes');
import d = require('./dog');

module Living.Things {
    // Why do I have to write b.Living.Things.Plant instead of b.Plant??
    class Tree extends b.Living.Things.Plant {
    }
}
export namespace Bookname{
export class Snows{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
export class Adventure{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
}





export namespace TreeList{
export class MangoTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
export class GuvavaTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
}
global3.ts

export namespace Living.Things {
  export class Animal {
    move() { /* ... */ }
  }
  export class Plant {
    photosynthesize() { /* ... */ }
  }
}
import b = require('./baseTypes');

export namespace Living.Things {
  // Error, can't find name 'Animal', ??
  export class Dog extends Animal {
    woof() { }
  }
}
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');

namespace Living.Things {
  // Why do I have to write b.Living.Things.Plant instead of b.Plant??
  class Tree extends b.Living.Things.Plant {

  }
}
export namespace A {
    export class Twix { ... }
}
export namespace A {
    export class PeanutButterCup { ... }
}
export namespace A {
     export class KitKat { ... }
}
namespace A {
    export class Twix { ... }
}
namespace A {
    export class PeanutButterCup { ... }
}
namespace A {
     export class KitKat { ... }
}
export class Twix { ... }
export class PeanutButterCup { ... }
export class KitKat { ... }
export default class SomeType {
  constructor() { ... }
}
function getThing() { return 'thing'; }
export default getThing;
export class SomeType { ... }
export function someFunc() { ... }
export namespace Animals {
  export class Dog { ... }
  export class Cat { ... }
}
export namespace Plants {
  export class Tree { ... }
}
export class Animal {
    move() { /* ... */ }
}

export class Plant {
    photosynthesize() { /* ... */ }
}
import b = require('./baseTypes');

export class Dog extends b.Animal {
    woof() { }
}   
import b = require('./baseTypes');

class Tree extends b.Plant {
}
import dog = require('./dog')
import tree = require('./tree')

export = {
    dog: dog,
    tree: tree
}
import LivingThings = require('./LivingThings');
console.log(LivingThings.Tree)
console.log(LivingThings.Dog)
export class Animal {
move() { /* ... */ }
}

export class Plant {
  photosynthesize() { /* ... */ }
}
import * as b from './base';

export class Dog extends b.Animal {
   woof() { }
} 
import { Dog } from './dog'

namespace things {
  export const dog = Dog;
}

export = things;
import * as things from './things';

console.log(things.dog);
import b = require('./baseTypes');

export module Living.Things {
    // Error, can't find name 'Animal', ??
    // Solved: can find, if properly referenced; exporting modules is useless, anyhow
    export class Dog extends b.Living.Things.Animal {
        public woof(): void {
            return;
        }
    }
}
// Error, can't use the same name twice, ??
// Solved: cannot declare let or const variable twice in same scope either: just use a different name
import b = require('./baseTypes');
import d = require('./dog');

module Living.Things {
    // Why do I have to write b.Living.Things.Plant instead of b.Plant??
    class Tree extends b.Living.Things.Plant {
    }
}
export namespace Bookname{
export class Snows{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
export class Adventure{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
}





export namespace TreeList{
export class MangoTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
export class GuvavaTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
}
此代码在全局范围内创建一个合并的命名空间
a

此设置很有用,但不适用于模块(因为模块不会污染全局范围)


版本3:无杯化 回到原始示例,cups
A
A
A
对您没有任何帮助。相反,您可以将代码编写为:

Mod1.ts

export namespace Living.Things {
  export class Animal {
    move() { /* ... */ }
  }
  export class Plant {
    photosynthesize() { /* ... */ }
  }
}
import b = require('./baseTypes');

export namespace Living.Things {
  // Error, can't find name 'Animal', ??
  export class Dog extends Animal {
    woof() { }
  }
}
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');

namespace Living.Things {
  // Why do I have to write b.Living.Things.Plant instead of b.Plant??
  class Tree extends b.Living.Things.Plant {

  }
}
export namespace A {
    export class Twix { ... }
}
export namespace A {
    export class PeanutButterCup { ... }
}
export namespace A {
     export class KitKat { ... }
}
namespace A {
    export class Twix { ... }
}
namespace A {
    export class PeanutButterCup { ... }
}
namespace A {
     export class KitKat { ... }
}
export class Twix { ... }
export class PeanutButterCup { ... }
export class KitKat { ... }
export default class SomeType {
  constructor() { ... }
}
function getThing() { return 'thing'; }
export default getThing;
export class SomeType { ... }
export function someFunc() { ... }
export namespace Animals {
  export class Dog { ... }
  export class Cat { ... }
}
export namespace Plants {
  export class Tree { ... }
}
export class Animal {
    move() { /* ... */ }
}

export class Plant {
    photosynthesize() { /* ... */ }
}
import b = require('./baseTypes');

export class Dog extends b.Animal {
    woof() { }
}   
import b = require('./baseTypes');

class Tree extends b.Plant {
}
import dog = require('./dog')
import tree = require('./tree')

export = {
    dog: dog,
    tree: tree
}
import LivingThings = require('./LivingThings');
console.log(LivingThings.Tree)
console.log(LivingThings.Dog)
export class Animal {
move() { /* ... */ }
}

export class Plant {
  photosynthesize() { /* ... */ }
}
import * as b from './base';

export class Dog extends b.Animal {
   woof() { }
} 
import { Dog } from './dog'

namespace things {
  export const dog = Dog;
}

export = things;
import * as things from './things';

console.log(things.dog);
import b = require('./baseTypes');

export module Living.Things {
    // Error, can't find name 'Animal', ??
    // Solved: can find, if properly referenced; exporting modules is useless, anyhow
    export class Dog extends b.Living.Things.Animal {
        public woof(): void {
            return;
        }
    }
}
// Error, can't use the same name twice, ??
// Solved: cannot declare let or const variable twice in same scope either: just use a different name
import b = require('./baseTypes');
import d = require('./dog');

module Living.Things {
    // Why do I have to write b.Living.Things.Plant instead of b.Plant??
    class Tree extends b.Living.Things.Plant {
    }
}
export namespace Bookname{
export class Snows{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
export class Adventure{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
}





export namespace TreeList{
export class MangoTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
export class GuvavaTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
}
Mod2.ts

export namespace Living.Things {
  export class Animal {
    move() { /* ... */ }
  }
  export class Plant {
    photosynthesize() { /* ... */ }
  }
}
import b = require('./baseTypes');

export namespace Living.Things {
  // Error, can't find name 'Animal', ??
  export class Dog extends Animal {
    woof() { }
  }
}
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');

namespace Living.Things {
  // Why do I have to write b.Living.Things.Plant instead of b.Plant??
  class Tree extends b.Living.Things.Plant {

  }
}
export namespace A {
    export class Twix { ... }
}
export namespace A {
    export class PeanutButterCup { ... }
}
export namespace A {
     export class KitKat { ... }
}
namespace A {
    export class Twix { ... }
}
namespace A {
    export class PeanutButterCup { ... }
}
namespace A {
     export class KitKat { ... }
}
export class Twix { ... }
export class PeanutButterCup { ... }
export class KitKat { ... }
export default class SomeType {
  constructor() { ... }
}
function getThing() { return 'thing'; }
export default getThing;
export class SomeType { ... }
export function someFunc() { ... }
export namespace Animals {
  export class Dog { ... }
  export class Cat { ... }
}
export namespace Plants {
  export class Tree { ... }
}
export class Animal {
    move() { /* ... */ }
}

export class Plant {
    photosynthesize() { /* ... */ }
}
import b = require('./baseTypes');

export class Dog extends b.Animal {
    woof() { }
}   
import b = require('./baseTypes');

class Tree extends b.Plant {
}
import dog = require('./dog')
import tree = require('./tree')

export = {
    dog: dog,
    tree: tree
}
import LivingThings = require('./LivingThings');
console.log(LivingThings.Tree)
console.log(LivingThings.Dog)
export class Animal {
move() { /* ... */ }
}

export class Plant {
  photosynthesize() { /* ... */ }
}
import * as b from './base';

export class Dog extends b.Animal {
   woof() { }
} 
import { Dog } from './dog'

namespace things {
  export const dog = Dog;
}

export = things;
import * as things from './things';

console.log(things.dog);
import b = require('./baseTypes');

export module Living.Things {
    // Error, can't find name 'Animal', ??
    // Solved: can find, if properly referenced; exporting modules is useless, anyhow
    export class Dog extends b.Living.Things.Animal {
        public woof(): void {
            return;
        }
    }
}
// Error, can't use the same name twice, ??
// Solved: cannot declare let or const variable twice in same scope either: just use a different name
import b = require('./baseTypes');
import d = require('./dog');

module Living.Things {
    // Why do I have to write b.Living.Things.Plant instead of b.Plant??
    class Tree extends b.Living.Things.Plant {
    }
}
export namespace Bookname{
export class Snows{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
export class Adventure{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
}





export namespace TreeList{
export class MangoTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
export class GuvavaTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
}
Mod3.ts

export namespace Living.Things {
  export class Animal {
    move() { /* ... */ }
  }
  export class Plant {
    photosynthesize() { /* ... */ }
  }
}
import b = require('./baseTypes');

export namespace Living.Things {
  // Error, can't find name 'Animal', ??
  export class Dog extends Animal {
    woof() { }
  }
}
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');

namespace Living.Things {
  // Why do I have to write b.Living.Things.Plant instead of b.Plant??
  class Tree extends b.Living.Things.Plant {

  }
}
export namespace A {
    export class Twix { ... }
}
export namespace A {
    export class PeanutButterCup { ... }
}
export namespace A {
     export class KitKat { ... }
}
namespace A {
    export class Twix { ... }
}
namespace A {
    export class PeanutButterCup { ... }
}
namespace A {
     export class KitKat { ... }
}
export class Twix { ... }
export class PeanutButterCup { ... }
export class KitKat { ... }
export default class SomeType {
  constructor() { ... }
}
function getThing() { return 'thing'; }
export default getThing;
export class SomeType { ... }
export function someFunc() { ... }
export namespace Animals {
  export class Dog { ... }
  export class Cat { ... }
}
export namespace Plants {
  export class Tree { ... }
}
export class Animal {
    move() { /* ... */ }
}

export class Plant {
    photosynthesize() { /* ... */ }
}
import b = require('./baseTypes');

export class Dog extends b.Animal {
    woof() { }
}   
import b = require('./baseTypes');

class Tree extends b.Plant {
}
import dog = require('./dog')
import tree = require('./tree')

export = {
    dog: dog,
    tree: tree
}
import LivingThings = require('./LivingThings');
console.log(LivingThings.Tree)
console.log(LivingThings.Dog)
export class Animal {
move() { /* ... */ }
}

export class Plant {
  photosynthesize() { /* ... */ }
}
import * as b from './base';

export class Dog extends b.Animal {
   woof() { }
} 
import { Dog } from './dog'

namespace things {
  export const dog = Dog;
}

export = things;
import * as things from './things';

console.log(things.dog);
import b = require('./baseTypes');

export module Living.Things {
    // Error, can't find name 'Animal', ??
    // Solved: can find, if properly referenced; exporting modules is useless, anyhow
    export class Dog extends b.Living.Things.Animal {
        public woof(): void {
            return;
        }
    }
}
// Error, can't use the same name twice, ??
// Solved: cannot declare let or const variable twice in same scope either: just use a different name
import b = require('./baseTypes');
import d = require('./dog');

module Living.Things {
    // Why do I have to write b.Living.Things.Plant instead of b.Plant??
    class Tree extends b.Living.Things.Plant {
    }
}
export namespace Bookname{
export class Snows{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
export class Adventure{
    name:any;
    constructor(bookname){
        console.log(bookname);
    }
}
}





export namespace TreeList{
export class MangoTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
export class GuvavaTree{
    name:any;
    constructor(treeName){
        console.log(treeName);
    }
}
}
要创建如下所示的图片:

好多了

现在,如果您仍在考虑您到底有多希望在模块中使用名称空间,请继续阅读


这些不是你想要的概念 我们首先需要回到名称空间的起源,并检查这些原因对于外部模块是否有意义

组织:名称空间便于将逻辑相关的对象和类型分组在一起。例如,在C#中,您将在
System.Collections
中找到所有集合类型。通过将类型组织到分层名称空间中,我们为这些类型的用户提供了良好的“发现”体验

名称冲突:名称空间对于避免命名冲突非常重要。例如,您可能有
My.Application.Customer.AddForm
My.Application.Order.AddForm
——两种类型的名称相同,但命名空间不同。在一种语言中,所有标识符都存在于同一根作用域中,并且所有程序集都加载所有类型,因此将所有内容都放在命名空间中是至关重要的

import { Shapes } from './square';
import { Shapes as _Shapes } from './triangle';

// ugh
let myTriangle = new _Shapes.Shapes.Triangle();
这些原因在外部模块中有意义吗

组织:外部模块已经存在于文件系统中。我们必须通过路径和文件名来解析它们,所以有一个逻辑组织方案供我们使用。我们可以有一个
/collections/generic/
文件夹,其中包含
列表
模块

名称冲突:这在外部模块中根本不适用。在一个模块中,没有合理的理由让两个对象具有相同的名称。从消费端来看,任何给定模块的消费者都可以选择他们将用于引用模块的名称,因此不可能出现意外的命名冲突


即使您不认为模块的工作方式充分解决了这些原因,但在外部模块中尝试使用名称空间的“解决方案”甚至不起作用

盒子里的盒子里的盒子 故事:

你的朋友鲍勃打电话给你。“我家里有一个很棒的新组织方案,”他说,“来看看吧!”。太好了,我们去看看鲍勃想出了什么主意

你从厨房开始,打开餐具室。有60个不同的盒子,每个盒子上都标有“餐具室”。你随便挑一个盒子,打开它。里面是一个标有“谷物”的盒子。你打开“谷物”盒子,找到一个标有“意大利面”的盒子。你打开“意大利面”盒子,发现一个标有“Penne”的盒子。你打开这个盒子,发现,正如你所期待的,一袋佩恩面食

有点困惑,你拿起一个相邻的盒子,上面也贴着“餐具室”的标签。里面是一个单独的盒子,同样标有“谷物”。你打开“谷物”盒子,再次找到一个标有“意大利面”的盒子。你打开“意大利面”盒子,发现有一个盒子,这个盒子的标签是“里加通心粉”。你打开这个盒子,发现。。。一袋里加通心粉

“太棒了!”鲍勃说。“所有内容都在名称空间中!”

“但是鲍勃……”你回答。“你的组织计划是无用的。你必须打开一堆盒子才能找到任何东西,而且实际上找东西并不比把所有东西都放在一个盒子里而不是三个盒子里更方便。事实上,既然你的食品储藏室已经一个架子一个架子地分类了,你根本不需要这些盒子。为什么不把意大利面放在桌子上呢?”小精灵,需要的时候去拿吧?”

“你不明白--我需要确保没有其他人将不属于“食品储藏室”名称空间的东西放入。我已经安全地将我所有的意大利面组织到
食品储藏室.Grains.意大利面
名称空间中,这样我就可以很容易地找到它”

鲍勃是一个非常困惑的人

模块是它们自己的盒子 在现实生活中,你可能也遇到过类似的情况:你在亚马逊上订购了几样东西,每样东西都显示在自己的盒子里,里面有一个较小的盒子,你的东西都包在自己的包装里。即使内部包装箱相似,装运也不是有用的“组合”

按照盒子类比,关键的观察结果是外部模块是它们自己的盒子。这可能是一个非常复杂的项目