Closed
Description
TypeScript Version: v4.0.5
Search Terms: typeof, cached typeof
Code
// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
interface TestHash {
[index: string]: boolean;
}
type TestType = boolean | TestHash;
function testTypeOf(value: TestType) {
const valueType = typeof value;
if (valueType === "boolean") {
return value;
}
if (valueType === "object") {
return Object.keys(value).every((key) => value[key]);
}
return !!value
}
Expected behavior:
The cached result of typeof
should also be used to narrow down the union type.
Actual behavior:
It errors with:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'TestType'. No index signature with a parameter of type 'string' was found on type 'TestType'.
Playground Link:
Related Issues:
Activity
MartinJohns commentedon Nov 21, 2020
Duplicate of #12184.