I am using typescript-eslint v3.1.0 and have the following type guard function in TypeScript:
interface JWTPayload {
sub: number;
}
function isJWTPayload(obj: unknown): obj is JWTPayload {
if (typeof obj !== 'object') {
return false;
}
// eslint-disable-next-line @typescript-eslint/ban-types
const obj2 = obj as object;
if (!('sub' in obj2)) {
return false;
}
const obj3 = obj2 as JWTPayload;
if (!Number.isInteger(obj3.sub)) {
return false;
}
return true;
}
My issue is this: it doesn't feel good to disable a lint rule in what I imagine is a common scenario. Is there a TypeScript pattern that would avoid this?
Some background:
The typescript-eslint rule that is being disabled was introduced in https://github.com/typescript-eslint/typescript-eslint/pull/848, in which it is opined that "99.9% of the time, you don't want to use [the "object" type], and the vast majority of codebases won't want to use it". Why might they say this? It seems like it would be used whenever you are validating user input. Is there some other way to do it without a cast to 'any'?