Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the naming convention for interfaces and classes in TypeScript?

Tags:

typescript

I have the following:

interface IUser {
  email: string
  password: string
}

class User {
  email: string
  password: string
  constructor(email: string, password: string) {
    this.email = email
    this.password = password
  }

  isEmailValid(): boolean {
    return validatorJs.isEmail(this.email)
  }

  isPasswordValid(): boolean {
    return validatorJs.isStrongPassword(this.password, opts)
  }
}

function createUser(user: IUser) {
  // applying isPasswordValid and isEmailValid
  //insert ...
}

function getUser(): IUser {
  return new User('[email protected]', 'foobar')
}

I have to put the letter "I" before the interface name, is that correct or should I do it differently?

like image 636
Diego Lope Loyola Avatar asked Jul 23 '21 18:07

Diego Lope Loyola


People also ask

WHAT IS interface and class in TypeScript?

TypeScript class vs.Classes are the fundamental entities used to create reusable components. It is a group of objects which have common properties. It can contain properties like fields, methods, constructors, etc. An Interface defines a structure which acts as a contract in our application.

How do you name an interface class?

Naming InterfacesInterfaces should be in the title case with the first letter of each separate word capitalized. In some cases, interfaces can be nouns as well when they present a family of classes e.g. List and Map .

CAN interface and class have the same name?

Case-3: When two interfaces contain a method with the same name, same signature but different return types, in this case, both interfaces can't be implemented in the same class.

Should interfaces be capitalized TypeScript?

By convention, the interface names are in the camel case. They use a single capitalized letter to separate words in there names.

What is the use of interface in typescript?

One of the most common uses of interfaces in languages like C# and Java, that of explicitly enforcing that a class meets a particular contract, is also possible in TypeScript. ts interfaceClockInterface{ currentTime: Date;

What are the fields of a class in typescript?

In plain JavaScript, there are no real fields, because the class members are always public; we simply call them properties. In TypeScript, you can define "true" C#-like properties (with encapsulation).

What happens when an interface type extends a class type?

When an interface type extends a class type it inherits the members of the class but not their implementations. It is as if the interface had declared all of the members of the class without providing an implementation.

What's the difference between a class and an interface in JavaScript?

The real difference comes when we consider our compiled JavaScript output. Unlike an interface, a class is also a JavaScript construct, and is much more than just a named piece of type information. The biggest difference between a class and an interface is that a class provides an implementation of something, not just its shape.


Video Answer


2 Answers

There's no official naming convention though it's common in the broader TS community to not use I for interfaces unless they're explicitly for being implemented by classes like:

import type { IUser } from '$models/interfaces/iuser.interface';
import type { IDeserializable } from '$models/interfaces/ideserializable.interface';

export class UserModel implements IDeserializable<IUser>, IUser {
  public email: string;
  public password: string;

  deserialize(input: IUser): this {
    Object.assign(this, input);
    return this;
  }
}

(Pattern adapted from sveltekit-starter by Navneet Sharma.)

So which naming method you choose for interfaces is ultimately at your own discretion.

like image 180
Ryan Cavanaugh Avatar answered Oct 21 '22 05:10

Ryan Cavanaugh


Someone has developed a TypeScript style guide. You can find it here: https://ts.dev/style

From that guide:

This is the style guide for the TypeScript language that was based on the one that is provided by Google. It contains both rules and best practices. Choose those that work best for your team.

This is what it says about interface naming:

Do not mark interfaces specially ( IMyInterface or MyFooInterface) unless it's idiomatic in its environment. When introducing an interface for a class, give it a name that expresses why the interface exists in the first place (e.g. class TodoItem and interface TodoItemStorage if the interface expresses the format used for storage/serialization in JSON).

like image 23
DeborahK Avatar answered Oct 21 '22 05:10

DeborahK