typescript - TypeScript 中接口(interface)和类的命名约定是什么?

标签 typescript

我有以下几点:

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('foo@bar.com', 'foobar')
}
我必须在接口(interface)名称之前加上字母“I”,这是正确的还是应该不同?

最佳答案

TypeScript 语言没有正式的命名约定,因此“正确”取决于您。在更广泛的 TS 社区中,不使用 I 是很常见的。对于接口(interface),除非它们明确用于由类实现(这不一定是 TypeScript 中接口(interface)的最常见用途)。

关于typescript - TypeScript 中接口(interface)和类的命名约定是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68503504/

相关文章:

typescript - 有没有办法实现 <T>(x : Promise<AsyncIterableIterator<T>>): AsyncIterableIterator<T> in TypeScript?

reactjs - 如何将 react with typescript 添加到现有的 asp.net core mvc 应用程序

javascript - 从另一个更新一个 React 的组件状态

javascript - 将字符串转换为盲文

angular - ionic2 - 在侧边菜单中添加注销

typescript - 在编译 typescript 时找不到“tsc 命令”

javascript - DefinelyTyped如何将声明文件绑定(bind)到JavaScript库?

debugging - 如何在 VS Code 中调试守夜人测试

Java Spring Security ionic4 HTTP 请求

typescript - 不使用 npm install -g 使用 tsc