I have some class with the following definition:
export class WorkspaceFilter {
[key: string]: boolean | string | [string]
}
Then I try to use it in the following function:
const fn = (filter: WorkspaceFilter, key: string) => {
if (Array.isArray(filter[key]) {
console.log(filter[key].length)
}
}
The error is Property 'length' does not exist on type 'false'.
Obviously, that's because boolean is one of the accepted types. But I'm checking the type manually! How to shut up Typescript after that exact js type check?
filter[key]and TS things it might be a different key. Extract to a local variableconst myFilter = filter[key]and use that