I have a string value, which I intend to use as an index of an array. I'm sure of this value and want to assert that it's a valid 'index value', w/o converting it to a number at runtime or asserting it's a number (b/c it's not a number).
So I have:
const inputEl = window.document.getElementById('foo') as HTMLInputElement;
//it's indeed a string, I'm sure of it and have no squirm because of it
const v = inputEl.value as string;
const a = ['foo',];
const result = a[v];
console.log(result);
Currently tsc
throws
Element implicitly has an 'any' type because index expression is not of type 'number' (tsserver 7015)
I'd rather not do something like:
//just because it's not a number
const v = inputEl.value as unknown as number;
Is there a better way to do it? Thank you.
Just don't use the input
.value
string. Use its.valueAsNumber
.