其他分享
首页 > 其他分享> > [Typescript] Defining a namespace alias

[Typescript] Defining a namespace alias

作者:互联网

namespace AllGreetings {
    export namespace Greetings {
        export function returnGreeting (greeting: string) {
            console.log(`The message from namespace Greetings is ${greeting}.`);
        }
    }
    export namespace GreetingsWithLength {
        export function returnGreeting (greeting: string) {
            let greetingLength = getLength(greeting);
            console.log(`The message from namespace GreetingsWithLength is ${greeting}. It is ${greetingLength} characters long.`);
        }
        function getLength(message: string): number {
            return message.length
        }
    }
}
AllGreetings.Greetings.returnGreeting('Bonjour');        // OK
AllGreetings.GreetingsWithLength.returnGreeting('Hola');  // OK

 

TypeScript creates an easy-to-navigate hierarchy of nested namespaces. But, as your nested namespaces become more complex, you may want to create an alias to shorten and simplify your code. To do this, use the import keyword.

import greet = AllGreetings.Greetings;
greet.returnGreeting('Bonjour');

 

标签:Typescript,namespace,greeting,export,returnGreeting,message,AllGreetings,Definin
来源: https://www.cnblogs.com/Answer1215/p/16669437.html