Basic Types Flashcards
What are the three main primitive types in TypeScript and how are they declared?
The three main primitives are string, number, and boolean. They are always lowercase in TypeScript.
let stringVar: string = “Hello”;
let numberVar: number = 42;
let booleanVar: boolean = true;
How do you declare arrays in TypeScript?
Arrays can be declared using two syntaxes: Type[] or Array<Type>.</Type>
let numbers: number[] = [1, 2, 3];
let strings: Array<string> = ["a", "b", "c"];
let mixed: (number | string)[] = [1, "two", 3];</string>
What is the ‘any’ type and how does noImplicitAny affect it?
‘any’ is a type that can hold any type of value. noImplicitAny is a compiler option that raises an error when variables have an implied ‘any’ type.
let anyValue: any = 4;
anyValue = “string”; // OK
anyValue = false; // OK
function fn(s) { // Error: Parameter ‘s’ implicitly has an ‘any’ type
console.log(s.subtr(3));
}
function fn(s: string) {
console.log(s.substr(3));
}
How do you explicitly annotate types on variables in TypeScript?
Type annotations come after a colon (:) following the variable name.
let myName: string = “John”;
let age: number = 30;
let isActive: boolean = true;
let x: number, y: string, z: boolean;
How do you work with function types in TypeScript?
TypeScript allows you to specify parameter types and return types for functions.
function greet(name: string) {
console.log(Hello, ${name}
);
}
function add(x: number, y: number): number {
return x + y;
}
let myAdd: (x: number, y: number) => number =
function(x, y) { return x + y; };
How do you define object types and optional properties?
Object types can be defined inline or through interfaces/type aliases.
let user: { name: string; age: number } = {
name: “John”,
age: 30
};
let contact: {
name: string;
phone?: string; // Optional property
} = {
name: “John” // phone is optional
};
How do you define and work with union types?
Union types allow a value to be one of several types.
let id: string | number;
id = “abc123”; // OK
id = 123; // OK
id = true; // Error
function processId(id: string | number) {
if (typeof id === “string”) {
console.log(id.toUpperCase()); // OK
} else {
console.log(id.toFixed(2)); // OK
}
}
What are type aliases and how are they used?
Type aliases create a new name for a type using the type keyword.
type Point = {
x: number;
y: number;
};
type ID = string | number;
let myPoint: Point = { x: 10, y: 20 };
let userId: ID = “abc123”;
What are interfaces and how do they differ from type aliases?
Interfaces define contracts in your code and can be extended.
interface Animal {
name: string;
age: number;
}
interface Dog extends Animal {
breed: string;
}
class Labrador implements Dog {
name: string;
age: number;
breed: string;
constructor(name: string, age: number) { this.name = name; this.age = age; this.breed = "Labrador"; } }
How do you use type assertions in TypeScript?
Type assertions tell the compiler to treat a value as a specific type.
let someValue: any = “this is a string”;
let strLength: number = (<string>someValue).length;</string>
let otherValue: any = “hello”;
let otherLength: number = (otherValue as string).length;
What are literal types and how does literal inference work?
Literal types are specific values of a type.
let direction: “north” | “south” | “east” | “west”;
direction = “north”; // OK
direction = “up”; // Error
const obj = { counter: 0 }; // Type is { counter: number }
const req = { url: “https://example.com”, method: “GET” }; // method: string
How does TypeScript handle null and undefined?
TypeScript has two special types: null and undefined.
let name: string = null; // OK
let name: string | null = null; // OK
let age: number | undefined; // OK
function getLength(str: string | null) {
return str!.length; // Assert str is non-null
}
What are enums and how are they used?
Enums allow you to define a set of named constants.
enum Direction {
Up = “UP”,
Down = “DOWN”,
Left = “LEFT”,
Right = “RIGHT”
}
let myDirection: Direction = Direction.Up;
enum StatusCodes {
OK = 200,
NotFound = 404,
Error = 500
}
What are some less common primitive types in TypeScript?
TypeScript includes several additional primitive types beyond string, number, and boolean.
let big: bigint = 100n;
let sym: symbol = Symbol(“key”);
function logMessage(): void {
console.log(“Hello”);
}
function throwError(message: string): never {
throw new Error(message);
}