TypeScript 4.4: control flow analysis of aliased conditions
Posted by Draupadi Rajkumari · 2021-08-31
TS 4.4 finally narrows types through aliased conditions. No more redundant type guards! This has been a paper cut for years. Also: exact optional properties prevent accidental undefined. https://github.com/microsoft/TypeScript/releases/tag/v4.4.2
// TypeScript 4.4 — aliased condition narrowing
function process(value: string | number) {
const isString = typeof value === 'string';
if (isString) {
// TS 4.4: value is narrowed to string here!
console.log(value.toUpperCase());
}
}
// Exact optional properties
interface Config {
debug?: boolean; // can't assign undefined explicitly now
}