首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >父类中的TypeScript方法子类中的访问类型

父类中的TypeScript方法子类中的访问类型
EN

Stack Overflow用户
提问于 2020-02-18 23:52:43
回答 2查看 35关注 0票数 0

我有一个类:

代码语言:javascript
复制
export class DataAccess {
  tableName: string;
  constructor({ tableName }: { tableName: string }) {
    this.tableName = tableName;
  }
  async getWhere(where: any, { single = false } = {}) {
    const options: { limit?: number } = {};
    if (single) {
      options.limit = 1;
    }
    const results = await someDBobj[this.tableName].find(where, options);
    if (!single) {
      return results;
    }
    return results.length ? results[0] : null;
  }
}

和一个子类:

代码语言:javascript
复制
import { DataAccess } from "./data-access";

type UsersTable = {
  id: string;
  email: string;
  password: string;
  createdAt: Date;
};

export default class Users extends DataAccess {
  columns: { [P in keyof UsersTable]: string };
  constructor() {
    super({
      tableName: "users"
    });
    this.columns = {
      id: "id",
      email: "email",
      password: "password",
      createdAt: "createdAt"
    };
  }
}

并使用Users类:

代码语言:javascript
复制
await users.getWhere(
  {
    email: user,
    password
  },
  {
    single: true
  }
);

如何在父DataAccess类中设置where: any的类型,以便它知道调用它的是Users子类?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-19 00:16:37

您可以使用泛型类

代码语言:javascript
复制
export class DataAccess<T> {
  columns: { [P in keyof T]: string };
  tableName: string;
  constructor({ tableName }: { tableName: string }) {
    this.tableName = tableName;
  }
  async getWhere(where: Partial<T>, { single = false } = {}) {
    const options: { limit?: number } = {};
    if (single) {
      options.limit = 1;
    }
    const results = await someDBobj[this.tableName].find(where, options);
    if (!single) {
      return results;
    }
    return results.length ? results[0] : null;
  }
}
代码语言:javascript
复制
import { DataAccess } from "./data-access";

type UsersTable = {
  id: string;
  email: string;
  password: string;
  createdAt: Date;
};

export default class Users extends DataAccess<UsersTable > {
  constructor() {
    super({
      tableName: "users"
    });
    this.columns = {
      id: "id",
      email: "email",
      password: "password",
      createdAt: "createdAt"
    };
  }
}
票数 1
EN

Stack Overflow用户

发布于 2020-02-19 00:21:44

看起来您只是为了获得数据形状的专门化而扩展了类。在这种情况下,您可以这样做:

代码语言:javascript
复制
class DataAccess<T> {
  columns: { [P in keyof T]: string };
  tableName: string;
  constructor({ tableName, columns }: { tableName: string, columns: { [P in keyof T]: string } }) {
    this.tableName = tableName;
    this.columns = columns;
  }
  async getWhere(where: Partial<T>, { single = false } = {}) {
    const options: { limit?: number } = {};
    if (single) {
      options.limit = 1;
    }
    const results = await someDBobj[this.tableName].find(where, options);
    if (!single) {
      return results;
    }
    return results.length ? results[0] : null;
  }
}

type UsersTable = {
  id: string;
  email: string;
  password: string;
  createdAt: Date;
};

const user = new DataAccess<UsersTable>({tableName: ""users", columns: {
  id: "id",
  email: "email",
  password: "password",
  createdAt: "createdAt"
};})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60284771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档