TypeScript Basic Types and Type Annotations Quiz

TypeScript
0 Passed
0% acceptance

A 40-question TypeScript quiz covering primitive types, type inference, explicit annotations, arrays, tuples, object types, unions, intersections, and literal types.

40 Questions
~80 minutes
1

Question 1

Which of the following is NOT a TypeScript primitive type?

A
object
B
string
C
number
D
boolean
2

Question 2

What is the type of the value 42 in TypeScript?

A
number
B
bigint
C
numeric
D
float
3

Question 3

What type does TypeScript infer here?

typescript
const flag = true;
A
boolean
B
true
C
string
D
unknown
4

Question 4

Which type represents extremely large integers?

A
bigint
B
huge
C
long
D
double
5

Question 5

Which keyword creates a unique, immutable primitive value?

A
symbol
B
unique
C
key
D
uuid
6

Question 6

What type is inferred?

typescript
let x = 2;
A
number
B
2
C
any
D
unknown
7

Question 7

When does TypeScript apply type inference?

A
When a variable is assigned without an explicit type
B
Only inside functions
C
Only on arrays
D
Only when using strict mode
8

Question 8

What is the inferred type?

typescript
const greeting = 'hello';
A
'hello'
B
string
C
any
D
unknown
9

Question 9

Why might explicit type annotations be useful?

A
They clarify intent when inference is insufficient
B
They increase runtime speed
C
They remove the need for compilation
D
They force TypeScript to skip checking
10

Question 10

What is the type of value here?

typescript
let id: string = 'abc123';
A
string
B
'abc123'
C
any
D
unknown
11

Question 11

Which annotation is valid for function return types?

A
: number
B
= number
C
-> number
D
return number
12

Question 12

What does this function return type annotation mean?

typescript
function sum(a: number, b: number): number { return a + b; }
A
The function must return a number
B
The function may return anything
C
The return type is ignored
D
The return type is never
13

Question 13

Which syntax creates an array of numbers?

A
number[]
B
num[]
C
[number]
D
array(number)
14

Question 14

What is the type of this array?

typescript
const list = [1, 2, 3];
A
number[]
B
(1 | 2 | 3)[]
C
any[]
D
unknown[]
15

Question 15

What is a tuple?

A
An array with a fixed length and element types
B
An array that can only contain strings
C
A dynamic array with unknown content
D
A mapped array type
16

Question 16

What is the type of this tuple?

typescript
const pair: [string, number] = ['age', 30];
A
[string, number]
B
string[]
C
(string | number)[]
D
tuple<string>
17

Question 17

What is structural typing?

A
Objects are typed by their shape rather than identity
B
Objects require class inheritance
C
Objects must share a constructor
D
Objects are typed by unique IDs
18

Question 18

What type is enforced?

typescript
const user: { name: string; age: number } = { name: 'Evan', age: 20 };
A
{ name: string; age: number }
B
any
C
{ name: string }
D
object
19

Question 19

How do you mark an optional property?

A
prop?: string
B
prop! string
C
prop? string
D
prop:optional string
20

Question 20

What is the type of this optional property?

typescript
type Info = { title?: string };
A
string | undefined
B
string
C
undefined only
D
never
21

Question 21

What does a union type represent?

A
A value that can be one of several types
B
A type merged with all others
C
A type that rejects all values
D
A numeric-only type
22

Question 22

What type is this variable?

typescript
let value: string | number = 'id';
A
string | number
B
string
C
any
D
unknown
23

Question 23

What is narrowing?

A
Reducing a union to a more specific type based on checks
B
Making variables smaller
C
Compiling multiple files at once
D
Combining two different types
24

Question 24

What type remains after this check?

typescript
let id: string | number = 10;
if (typeof id === 'string') {
  // inside
}
A
string
B
number
C
any
D
never
25

Question 25

What does an intersection type do?

A
Combines multiple types into one containing all properties
B
Chooses between one type or another
C
Prevents objects from being merged
D
Clears existing type information
26

Question 26

What type is produced by this intersection?

typescript
type A = { x: number };
type B = { y: string };
type C = A & B;
A
{ x: number; y: string }
B
{ x: number }
C
{ y: string }
D
never
27

Question 27

Which statement about intersections is correct?

A
They require values to meet all constituent type constraints
B
They eliminate all properties
C
They behave exactly like unions
D
They automatically widen types
28

Question 28

What is a literal type?

A
A type restricted to a specific value
B
A type with unlimited values
C
Any primitive type
D
A type created at runtime
29

Question 29

Which of these is a literal type?

A
'ready'
B
string
C
boolean
D
number
30

Question 30

What type is declared here?

typescript
let direction: 'left' | 'right';
A
'left' | 'right'
B
string
C
boolean
D
unknown
31

Question 31

What is the inferred type?

typescript
const status = 'ok' as const;
A
'ok'
B
string
C
any
D
'ok' | 'fail'
32

Question 32

What is a common use of literal types?

A
Creating controlled sets of allowed values
B
Running code faster
C
Forcing type inference off
D
Defining nullable objects
33

Question 33

Which array annotation uses generic syntax?

A
Array<number>
B
numberArray
C
Number[]
D
[number]
34

Question 34

What is the inferred type?

typescript
const roles = ['admin', 'user'] as const;
A
readonly ['admin', 'user']
B
string[]
C
any[]
D
readonly string[]
35

Question 35

What type describes an object with unknown keys but number values?

A
{ [key: string]: number }
B
object<number>
C
any
D
Map<string, number>
36

Question 36

What is the type of result?

typescript
const id = Math.random() > 0.5 ? 10 : 'ten';
A
string | number
B
number
C
string
D
any
37

Question 37

Which describes a readonly tuple?

A
It prevents modification of its elements
B
It expands automatically
C
It allows any value
D
It is the same as a normal array
38

Question 38

What type does this infer?

typescript
let result = [1, 'a'];
A
(string | number)[]
B
[number, string]
C
unknown[]
D
string[]
39

Question 39

Which statement about any is true?

A
It disables type checking for that value
B
It prevents runtime errors
C
It makes code faster
D
It is safer than unknown
40

Question 40

What does TypeScript infer for this function?

typescript
function greet() { return 'Hello'; }
A
string
B
any
C
void
D
unknown

QUIZZES IN TypeScript