Basic Types You'll See Everywhere
Basic Types You'll See Everywhere
Every piece of TypeScript code uses types. This tutorial covers the ones you'll see most often when reading AI-generated code.
The Primitive Types
These are the simplest building blocks:
// String — text let name: string = "Alice"; let greeting: string = `Hello, ${name}`; // Number — integers and decimals let age: number = 28; let price: number = 9.99; // Boolean — true or false let isLoggedIn: boolean = true; let hasPermission: boolean = false;
You'll see these three everywhere. They're the foundation of everything else.
Arrays
Arrays hold multiple values of the same type:
// Array of strings let fruits: string[] = ["apple", "banana", "cherry"]; // Array of numbers let scores: number[] = [95, 87, 92, 100]; // Array of booleans let flags: boolean[] = [true, false, true];
You might also see this alternative syntax (means the same thing):
let fruits: Array<string> = ["apple", "banana", "cherry"];
What to ask your AI: "I see
Array<string>in the code. Is that the same asstring[]? Which should I use?"
Objects
Objects group related data together. In TypeScript, you define their shape:
// Inline object type let user: { name: string; age: number } = { name: "Alice", age: 28, }; // More commonly, you'll see this defined as an interface (covered next tutorial)
When AI generates code, objects are usually defined with interfaces or type aliases — we'll cover those in the next tutorial. For now, know that the curly braces { } with types inside describe an object's shape.
Union Types — "This OR That"
Sometimes a value can be more than one type. The | symbol means "or":
// Can be a string or a number let id: string | number = "abc123"; id = 42; // Also valid! // Can be a string or null (very common) let userName: string | null = null; userName = "Alice"; // Also valid
You'll see union types constantly in AI-generated code, especially:
// Data that might not exist yet let data: User | null = null; // Loading states let status: "loading" | "success" | "error" = "loading";
That last example is called a literal type — the value can only be one of those exact strings.
What to ask your AI: "I see
string | nullin the code. Why is it allowing null? When would this be null?"
Special Types
any — The Escape Hatch
any turns off type checking for that value. It's like going back to JavaScript:
let whatever: any = "hello"; whatever = 42; // No error whatever = true; // No error whatever.foo.bar; // No error (but might crash at runtime!)
When you see any in AI-generated code, it often means the AI wasn't sure what type to use. You can ask it to be more specific:
What to ask your AI: "I see
anyused here: [paste code]. Can you replace it with a more specific type?"
undefined and null
undefined— a variable that exists but has no valuenull— a variable that intentionally has "no value"
let notSet: undefined = undefined; let empty: null = null;
In practice, you'll see these in union types: string | undefined or User | null.
void — No Return Value
Used for functions that don't return anything:
function logMessage(message: string): void { console.log(message); // No return statement }
Type Inference — TypeScript Figures It Out
Here's good news: you don't always need to write types explicitly. TypeScript can infer them:
// TypeScript knows this is a string — no annotation needed let name = "Alice"; // TypeScript knows this is a number let age = 28; // TypeScript knows this returns a number function add(a: number, b: number) { return a + b; // Return type inferred as 'number' }
When reading AI-generated code, you'll see a mix — sometimes types are explicit, sometimes inferred. Both are correct.
Optional Values with ?
The question mark makes a property or parameter optional:
// Optional object property let user: { name: string; nickname?: string } = { name: "Alice", // nickname is optional — we can leave it out }; // Optional function parameter function greet(name: string, greeting?: string) { return `${greeting || "Hello"}, ${name}!`; } greet("Alice"); // "Hello, Alice!" greet("Alice", "Hey"); // "Hey, Alice!"
The ? is one of the most common symbols you'll see in TypeScript. It simply means "this might not be provided."
What to ask your AI: "What's the difference between
name?: stringandname: string | undefined?"
Quick Reference
| Type | Example | Description |
|---|---|---|
string | "hello" | Text |
number | 42, 3.14 | Numbers |
boolean | true, false | True or false |
string[] | ["a", "b"] | Array of strings |
number[] | [1, 2, 3] | Array of numbers |
any | anything | Disables type checking |
void | — | No return value |
null | null | Intentionally empty |
undefined | undefined | Not yet assigned |
string | number | "hi" or 42 | Union (either type) |
? | name?: string | Optional |
What's Next?
Now that you know the basic types, the next tutorial covers interfaces and type aliases — how TypeScript defines the shape of more complex data structures.
What to ask your AI: "I'm reading some TypeScript and I see a type I don't recognize: [paste it]. Can you explain what it means?"