简体   繁体   中英

TypeScript - Use PascalCasing or camelCasing for module names?

I'm wondering if I should use PascalCasing or camelCasing for my modules names, so far I've always used PascalCasing, ie: Ayolan.Engine.Rendering instead of Ayolan.engine.rendering (I keep PascalCasing for the container, since I want the global object name to be Ayolan and not ayolan ).

I haven't found any headline on this, found that thread from 3 years ago but not really useful.

I'm wondering because I'm working with Java developpers and to them it makes more sense to use camelCasing , but that's not what I've seen so far with TS.

In TypeScript, we use the same standards as JavaScript, because we are working with many JavaScript libraries (and potentially being consumed by JavaScript code too).

So we prefer PascalCase for modules and classes, with members being camelCase.

module ExampleModule {
    export class ExampleClass {
        public exampleProperty: string;

        public exampleMethod() {

        }
    }
}

The only other style rule I can think of is that constants are ALL_UPPER.

You will notice that this blends in nicely with the following code:

Math.ceil(Math.PI);

Most importantly of all - keep consistent with the style you use as the style can imply meaning, so if you aren't consistent it will cause confusion.

Notice: Internal modules are namespaces. The next version of TypeScript focuses on external modules from ECMAScript 6 , which are not namespaces.

Microsoft didn't publish a naming guideline for internal modules in TypeScript.

Arguments in favor of PascalCase

An internal module is like a class with only static members:

module MyLib {
    export function f1() {
    }
    export function f2() {
    }
    // ...
}

Languages that use pascal case for namespaces: C# , PHP .

Arguments in favor of camelCase

A name in camel case is reminiscent of the Google Style Guide for JSON :

Property names must be camel-cased, ascii strings.

… but an internal module is not a JSON object.

Languages that use camel case for namespaces: ? (Not Java, which uses lowercase).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM