Below is the shortened code snippet causing an error:
export default function formatSql(this: EscapeFunctions, sqlQuery: string, values: QueryParams) {
if (isPlainObject(values)) {
console.log(values[p]); // <-- Element implicitly has an 'any' type because type 'QueryParams' has no index signature.
} else if (Array.isArray(values)) {
// ...
} else {
throw new Error(`Unsupported values type`);
}
// ...
}
QueryParams
is defined as:
export type QueryParams = StringMap | any[];
export interface StringMap {
[_:string]: any,
}
Therefore, StringMap
has an "index signature" according to the definition, and isPlainObject
is defined as:
export function isPlainObject(obj: any): obj is object {
return isObject(obj) && (
obj.constructor === Object // obj = {}
|| obj.constructor === undefined // obj = Object.create(null)
);
}
Despite setting isPlainObject
to return obj is StringMap
, Typescript still raises an issue.
Why is this happening? Is there a solution to avoid typecasting all elements?