Welcome back! I am Mihir, and in this lesson we will learn the string type in TypeScript.
A string is used for text. Names, emails, titles, slugs, messages, and URLs are usually strings.
What is a string?
A string is a sequence of characters.
let name: string = "Mihir";
let course: string = "TypeScript";
let email: string = "mihir@example.com";TypeScript checks that these variables always hold text.
Invalid:
let username: string = 100;100 is a number, so TypeScript gives an error.
String Syntax
You can create strings using double quotes:
let title: string = "Learn TypeScript";Single quotes:
let status: string = 'published';Template literals:
let framework: string = `Next.js`;All three create string values.
Type Inference with Strings
You do not always need to write : string.
let course = "TypeScript";TypeScript infers that course is a string.
This is invalid:
course = 50;Because TypeScript already knows course should be a string.
const String Inference
let and const behave differently.
let role = "admin";TypeScript infers role as:
stringBecause role can be reassigned.
const fixedRole = "admin";TypeScript infers fixedRole as:
"admin"This is called a string literal type. The value is exactly "admin".
Template Literals
Template literals let you insert values inside strings.
let firstName: string = "Mihir";
let courseName: string = "TypeScript";
let message: string = `Hello ${firstName}, welcome to ${courseName}.`;Template literals are useful when building messages:
function createWelcomeMessage(name: string): string {
return `Welcome, ${name}!`;
}The function takes a string and returns a string.
String Methods
TypeScript understands JavaScript string methods.
let topic: string = "typescript";
console.log(topic.toUpperCase());
console.log(topic.includes("script"));
console.log(topic.length);Because topic is a string, TypeScript allows string methods like:
toUpperCase()toLowerCase()includes()trim()slice()replace()
Invalid:
let count: number = 10;
count.toUpperCase();Numbers do not have toUpperCase(), so TypeScript warns.
Function Parameters with string
Function parameters should usually be annotated.
function greet(name: string): string {
return `Hello, ${name}`;
}
greet("Mihir");Invalid:
greet(100);The function expects a string.
Object Properties with string
String types are common inside object shapes.
type User = {
name: string;
email: string;
role: string;
};
const user: User = {
name: "Mihir",
email: "mihir@example.com",
role: "admin",
};This ensures each property has the correct value type.
String Literal Types
Sometimes you do not want any string. You want specific string values.
let status: "draft" | "published" | "archived";
status = "draft";
status = "published";Invalid:
status = "deleted";"deleted" is not one of the allowed string values.
This is useful for statuses, roles, themes, and modes.
Common Mistake: string vs String
Use lowercase string:
let name: string = "Mihir";Avoid uppercase String:
let name: String = "Mihir";For normal text values, lowercase string is the correct choice.
Common Mistake: Numeric Strings
This is a string:
let price: string = "999";This is a number:
let price: number = 999;If you need to calculate with the value, use number.
let total = 999 + 100;If you need to display or store text, use string.
Quick Reference Summary
| Concept | Example |
|---|---|
| String annotation | let name: string = "Mihir" |
| String inference | let name = "Mihir" |
| Template literal | `Hello ${name}` |
| Function parameter | function greet(name: string) |
| Function return | function greet(): string |
| Literal type | "draft" | "published" |
| Preferred primitive | string, not String |
Practice
Create a function that formats a course title:
function formatCourseTitle(title: string, category: string): string {
return `${category}: ${title.toUpperCase()}`;
}
const result = formatCourseTitle("type annotations", "TypeScript");
console.log(result);Try passing a number as the first argument and check TypeScript's warning.
What You've Learned
You now understand:
- What the
stringtype is - How TypeScript infers strings
- How
letandconststring inference differs - How template literals work
- How TypeScript checks string methods
- How to use strings in functions and objects
- How string literal types restrict allowed values
- Why lowercase
stringis preferred
What's Next?
In the next lesson, we will learn the number Type in TypeScript.
We will cover integers, decimals, calculations, NaN, Infinity, and common number mistakes.
Need Help?
- Have questions, confusion, or want to know more? Contact me
Strings are everywhere in real projects. TypeScript makes them much safer by catching wrong values early.