ashwanikumarjha
Fri Nov 29 2024
Definite Assignment Checks in TypeScript 5
• Before TypeScript 5:
• After TypeScript 5:
• The
• Use of
• Before TypeScript 5:
let value: string;
console.log(value); // TS4.x: This was allowed but could lead to runtime undefined
• After TypeScript 5:
let value: string;
console.log(value); // TS5.x: Error - Variable 'value' is used before being assigned
// To fix, either initialize:
let value = "initial";
// Or use definite assignment assertion:
let value!: string;
• The
!
is a promise to TypeScript that we'll assign a value before using it• Use of
!
should be avoided as it bypasses TypeScript's safety checks