Basic Types Flashcards

1
Q

What are the three main primitive types in TypeScript and how are they declared?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you declare arrays in TypeScript?

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the ‘any’ type and how does noImplicitAny affect it?

A

‘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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you explicitly annotate types on variables in TypeScript?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you work with function types in TypeScript?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you define object types and optional properties?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you define and work with union types?

A

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
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are type aliases and how are they used?

A

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”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are interfaces and how do they differ from type aliases?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you use type assertions in TypeScript?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are literal types and how does literal inference work?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does TypeScript handle null and undefined?

A

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
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are enums and how are they used?

A

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
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are some less common primitive types in TypeScript?

A

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);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly