0

I'm trying to translate my JavaScript code to TypeScript. I don't know how to translate that code:

const COLOR_WHITE = 0;
const COLOR_BLACK = 1;

const CASTLE_TYPE_SHORT = 0;
const CASTLE_TYPE_LONG = 1;

class Game {
    constructor() {
        this.castling_possibilities = {
            COLOR_WHITE: {
                CASTLE_TYPE_SHORT: true,
                CASTLE_TYPE_LONG: true
            },
            COLOR_BLACK: {
                CASTLE_TYPE_SHORT: true,
                CASTLE_TYPE_LONG: true
            }
        }
    }

    getCastlingPossibility(color, type) {
        return this.castling_possibilities[color][type];
    }
}

I got this code, but there are a lot of errors:

enum Color {
    White,
    Black
}

enum CastleType {
    Short,
    Long
}

class Game {
    castling_possibilities: Object /* what member type is required? */

    constructor() {
        this.castling_possibilities = {
            Color.White: {
                CastleType.Short: true,
                CastleType.Long: true
            },
            Color.Black: {
                CastleType.Short: true,
                CastleType.Long: true
            }
        }
    }

    getCastlingPossibility(color: Color, type: CastleType) : boolean {
        return this.castling_possibilities[color][type];
    }
}

I want something like an association array of Color and an association array of CastleType, but I don't know how to do that. What type of castling_possibilities do I need? Thanks in advance!

2 Answers 2

1

Firstly, you shouldn't use a numeric enum because it's unsafe (you can assign any number to it). Use a string enum or string union instead.

Here is a solution using string enum:

enum Color {
    White = 'white',
    Black = 'black',
}

enum CastleType {
    Short = 'short',
    Long = 'long',
}

type Dict = {
  [U in Color]: {
    [T in CastleType]: boolean;
  };
}

class Game {
    castling_possibilities: Dict

    constructor() {
        this.castling_possibilities = {
            [Color.White]: {
                [CastleType.Short]: true,
                [CastleType.Long]: true
            },
            [Color.Black]: {
                [CastleType.Short]: true,
                [CastleType.Long]: true
            }
        }
    }

    getCastlingPossibility(color: Color, type: CastleType) : boolean {
        return this.castling_possibilities[color][type];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
class Game {
  castling_possibilities: Partial<Record<Color, Partial<Record<CastleType, boolean>>>>;

  constructor() {
    this.castling_possibilities = {
      [Color.White]: { [CastleType.Long]: true, [CastleType.Short]: false },
      [Color.Black]: { [CastleType.Long]: false, [CastleType.Short]: true }
    };
  }

  getCastlingPossibility(color: Color, type: CastleType): boolean {
    return this.castling_possibilities[color]![type]!;
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.