Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

TypeScript Variables: Type Inference, const, let and Annotations

Type Inference

TypeScript often understands a variable type from the assigned value. This is called type inference. If you write const course = "TypeScript", TypeScript knows the value is a string-like value without needing an annotation.

Use inference when the type is obvious. It keeps code cleaner and avoids repeating information that TypeScript already knows. Good TypeScript code is not the code with the most annotations; it is the code where the important contracts are clear.

DeclarationInferred TypeReason
const course = "TypeScript""TypeScript"The binding cannot be reassigned.
let lessons = 15numberThe variable can later hold another number.
let published = truebooleanThe variable can later hold true or false.
Inferred Variables
const course = "TypeScript";
let lessons = 15;
let published = true;

lessons = 16;
published = false;

// lessons = "fifteen"; // Error
console.log(course, lessons, published);

Type Annotations

A type annotation explicitly tells TypeScript what type a variable should hold. Annotations are helpful when a variable starts empty, when a function returns a broad type, or when you want to document intent clearly.

Avoid annotating every obvious value. For example, const name: string = "Admin" is usually less useful than letting TypeScript infer it. Save annotations for places where they add clarity or safety.

Explicit Annotation
let username: string;
let totalViews: number = 0;
let tags: string[] = [];

username = "admin";
totalViews += 100;
tags.push("typescript");

let, const, and Literal Types

const means the variable binding cannot be reassigned. TypeScript can often infer a narrower literal type for const values because the value cannot change.

let allows reassignment, so TypeScript usually infers a wider type. This difference matters when you want only specific string or number values to be allowed.

Literal Inference
const role = "admin"; // type is "admin"
let status = "draft"; // type is string

type PostStatus = "draft" | "published" | "archived";
let postStatus: PostStatus = "draft";

postStatus = "published";
// postStatus = "deleted"; // Error

Working with Unknown Initial Values

Sometimes a variable is declared before its value is known. In that case, add an annotation so TypeScript understands what the variable will eventually contain.

If the value may genuinely be missing, include null or undefined in the type and handle that case before using the value. This makes missing data visible instead of accidental.

Nullable Variable
type User = { id: number; name: string };

let selectedUser: User | null = null;

function selectUser(user: User): void {
  selectedUser = user;
}

if (selectedUser === null) {
  console.log("No user selected");
} else {
  console.log(selectedUser.name);
}

Arrays, Objects, and Empty Values

Empty arrays and empty objects often need help from annotations. Without context, TypeScript may infer a type that is too narrow or not useful for the values you plan to store later.

Name important object shapes with an interface or type alias. This keeps variables readable and prevents the same inline object type from being repeated across several files.

Typed Empty Values
type Tutorial = {
  id: number;
  title: string;
  published: boolean;
};

const tutorials: Tutorial[] = [];

tutorials.push({
  id: 1,
  title: "TypeScript Variables",
  published: true,
});

Avoiding any

any turns off type checking for a value. It can be useful during migration from JavaScript or when temporarily integrating untyped libraries, but it should not become the default escape hatch.

Prefer unknown for values that truly come from outside your program. With unknown, TypeScript forces you to check the value before using it.

unknown Instead of any
let responseData: unknown = JSON.parse('{"name":"Admin"}');

if (
  typeof responseData === "object" &&
  responseData !== null &&
  "name" in responseData
) {
  console.log(responseData.name);
}

Good Variable Typing Habits

Good TypeScript does not mean adding the most annotations possible. It means adding useful information where it improves safety or communication.

Let local values infer naturally, annotate public function inputs and outputs, and name important object shapes with interfaces or type aliases.

  • Use const by default when a variable is not reassigned.
  • Use annotations for empty arrays, nullable values, and public contracts.
  • Avoid any unless you are intentionally escaping type checking.
  • Prefer readable domain types over repeated inline object annotations.
  • Use literal unions for variables that should accept only a small set of values.
Key Takeaways
  • TypeScript infers obvious variable types automatically.
  • Use annotations when values start empty or intent is not clear.
  • const can infer narrower literal types than let.
  • Represent missing values explicitly with null or undefined when needed.
  • Prefer unknown over any for uncertain external data.
  • Good typing adds useful contracts without cluttering every line.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.