// *** before: ***
const key = "count" as const;
function read(obj: Record<string, string | number>) {
if (typeof obj[key] === "number") {
// obj[key] is string | number again at the next access
return obj[key] + 1; // <-Error: obj[key] may be a string
}
return 0;
}
// *** in version 5.5: ***
function readNarrowed(obj: Record<string, string | number>) {
if (typeof obj[key] === "number") {
return obj[key] + 1; // OK: the narrowing holds for the same constant
//key
}
return 0;
}