Variables in TypeScript
In TypeScript, variables are explicitly declared and used by the compiler to check type-correctness of function calls.
To run this TypeScript code, you need to compile it to JavaScript first:
In TypeScript:
- Variables are typically declared using
let
or const
. - Type annotations are optional but can be added for clarity.
- TypeScript has type inference, so it can deduce types from initialized variables.
- Variables declared without initialization are
undefined
, not zero-valued. - The
const
keyword is used for variables that won’t be reassigned, similar to :=
in some cases.
TypeScript provides more flexibility in variable declarations compared to statically typed languages, while still offering strong type checking.