获取类变量作为json对象的typescript方法

vlju58qv  于 2022-12-14  发布在  TypeScript
关注(0)|答案(2)|浏览(172)

除了在类中创建一个函数以将类变量作为对象返回之外,是否有一种更简单的方法或内置的方法来从类中只获取公共变量而不获取函数或私有变量

有没有办法得到类似PokerObject的对象

常量表变量作为对象=表数组[1]
"而不是打电话"
常量表变量作为对象=表数组[1].getAsObject()

interface PokerObject {
  smallBlind: number;
  bigBlind: number;
  size: number;
}
interface PokerTable {
  smallBlind: number;
  bigBlind: number;
  size: number;
  GetNextPlayer: (extra: number) => number;
  getAsObject: () => PokerObject;
}
class Poker_Table implements PokerTable {
  public smallBlind: number;
  public size: number;
  public bigBlind: number;
  private id: string;
  constructor(id: string, size: number, smallBlind: number, bigBlind: number) {
    this.id = id;
    this.smallBlind = smallBlind;
    this.bigBlind = bigBlind;
    this.size = size;
  }
  getAsObject(): PokerObject {
    return {
      smallBlind: this.smallBlind,
      size: this.size,
      bigBlind: this.bigBlind,
    };
  }

  GetNextPlayer(extra: number): number {
    //retutrn sum number
    return 1;
  }
}

将类放入数组中

const Table1: PokerTable = new Poker_Table("1", 2, 1, 1);
const Table2: PokerTable = new Poker_Table("1", 2, 1, 1);
const Table3: PokerTable = new Poker_Table("1", 2, 1, 1);
const TablesArray: Array<PokerTable> = [Table1, Table2, Table3];
omjgkv6w

omjgkv6w1#

可以使用静态方法设置值,而不是在构造函数中设置值
例如,我在这里设置create方法来创建新PokerTable

static create(id: string, size: number, smallBlind: number, bigBlind: number){
    const pokerTable = new Poker_Table()
    pokerTable.id = id;
    pokerTable.smallBlind = smallBlind;
    pokerTable.bigBlind = bigBlind;
    pokerTable.size = size;
    return pokerTable

  // or 

   return pokerTable.getAsObject()
  }

当我返回它时,它不会返回私有值(只返回公共值和方法值)
而要将其作为对象获取(不使用任何方法),可以使用spread syntax
就像这样:

const Table1 = {...Poker_Table.create("1", 2, 1, 1)};

这里是打字机游戏场

7ivaypg9

7ivaypg92#

Playground

import { F } from 'ts-toolbelt'

interface PokerObject {
    smallBlind: number;
    bigBlind: number;
    size: number;
}
interface PokerTable {
    smallBlind: number;
    bigBlind: number;
    size: number;
    GetNextPlayer: (extra: number) => number;
}
class Poker_Table implements PokerTable {
    public smallBlind: number;
    public size: number;
    public bigBlind: number;
    private id: string;
    private nonEnumerableId!: string;
    constructor(id: string, size: number, smallBlind: number, bigBlind: number) {
        this.id = id;
        this.smallBlind = smallBlind;
        this.bigBlind = bigBlind;
        this.size = size;
        // make nonEnumerableId nonEnumerable
        Object.defineProperty(this, 'nonEnumerableId',
            {configurable: true, enumerable: false, writable: true, value: id})
    }
    GetNextPlayer(extra: number): number {
        //retutrn sum number
        return 1;
    }
    
    toJSON(): PokerObject {
        return pickAll(this as PokerObject, ['bigBlind', 'size', 'smallBlind'])
    }
}

function pickAll<T, K extends (keyof T)[]>(
    obj: T, keys: keyof T extends K[number] ? K : F.NoInfer<[...K, Exclude<keyof T, K[number]>]>
): T {
    return Object.fromEntries(keys.map(k => [k, obj[k]])) as any
}

let p = new Poker_Table('asd', 123, 234, 456)
console.log('p:', p)
// has id but not nonEnumerableId
console.log('{...p}:', {...p})
// has no ids
console.log('toJson:', p.toJSON())
// has no ids, is a string
console.log('stringify:', JSON.stringify(p))

相关问题