Just getting started with Typescript and currently working on a sudoku game. Here are the types and interface I have set up:
export type GridCellValue = 1|2|3|4|5|6|7|8|9;
export interface GridCell {
readonly: boolean,
value: GridCellValue|null,
}
export type Grid = Array<Array<GridCell>>
The cells in the sudoku grid are represented by HTMLInputElement
s that trigger an InputEvent
when their input changes (using Vue's @input
directive).
My current validation function looks like this :
function validate_input(ev: InputEvent) {
const input_string: string = ev.data ?? '';
const grid_cell_value_regex = /^([123456789])$/;
const value = grid_cell_value_regex.test(input_string) ? input_string : '';
if (ev.target === null)
return;
const el = ev.target as HTMLInputElement;
el.value = value;
}
I'm wondering if there is a better way to check if input_string
can be converted to a valid GridCellValue
, without relying on Regex. Is there another approach that could be more efficient?
I've already tried using instanceof
and is
for checking but it didn't yield the desired results.