I want the following:
const a: unknown = 'this is a string';
const stringType = 'string';
if (typeof a === stringType){
console.log(a.toLocaleUpperCase())
}
But it gives me an error: 'a' is of type 'unknown'
I know it works if I just compare typeof a
against 'string'
but I want to compare it against a const variable. How do I achieve this? Is there some tsconfig.json
option?
P.S. The reason for this is to optimise my code. This code is supposed to run thousands of times per single user request, so I’d rather compare it against an already defined variable than a newly created string every time