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?
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.
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 .
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.
By convention, the interface names are in the camel case. They use a single capitalized letter to separate words in there names.
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;
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).
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.
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.
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.
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 (
IMyInterfaceorMyFooInterface) 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With