Is there a naming convention for TypeScript interfaces?

The TypeScript language was created from Microsoft as a super-set of typed-rules for JavaScript. The language contains features that one finds in C#, something that is completely understandable since the inventor of TypeScript also invented C#.

TypeScript provides interfaces that are used for two main reasons:

  1. Standardize/ Giving a type to an object, by defining the properties that an object contains.

  2. Define a structure of properties and/ or functions that a class has to implement. Doing this way you can define multiple implementations that implement the interface. In this case, the interface works as a contract (known best practice term from C#) that the classes have to respect.

So now the question is, how should you name your interfaces? Should you use the “I” letter at the beginning of the name of the interface or not?

The “I”-naming convention is a best practice in C#. JavaScript has no interfaces so for this language there is no naming convention. Some people or books suggest you not to use this convention.

When the purpose of your interface is the first point from the previous list, then I can understand if you choose not to use the “I” letter. But if you use an interface as a contract for a set of relevant classes, then I use the “I”-naming convention and I suggest you doing the same. Here are some advantages of doing so:

  1. By looking on the name of the file you can directly understand if the file contains a class or an interface, without having to open the file to find it out. Apart from that you can also gather your TypeScript interfaces into a folder (possible names could be “Definitions”, “Contracts” or “Interfaces”) so that you do not mix them with the files that contain your classes.

  2. Coming from the C# world is easier for you to adapt to this convention, since you already used it like hundred times.

  3. When naming the classes that implement an interface you can simple remove the “I” letter from the name of the interface, leave the remaining intact and just add at the beginning the words that uniquely identify your class.

comments powered by Disqus