Type Annotation and Inference in TypeScript

This quiz will cover topics related to Type Annotation and Inference in TypeScript

Last Updated :
Discuss
Comments

Question 1

What is Type Annotation in TypeScript?

  • A way to assign a default value to a variable

  • A method to explicitly declare the type of a variable

  • A feature that automatically assigns types to variables

  • A way to convert one type into another

Question 2

What is Type Inference in TypeScript?

  • When TypeScript automatically determines the type of a variable

  • When TypeScript forces developers to specify types manually

  • A process of converting one type to another

  • A way to disable type checking

Question 3

What will be the inferred type of the variable x in the following code?

TypeScript
let x = 10;


  • number

  • any

  • unknown

  • string

Question 4

What happens if you try to assign a string to a variable inferred as a number?

TypeScript
let x = 100;
x = "Hello";
  • The code compiles and runs without errors

  • TypeScript throws a compilation error

  • TypeScript changes the type of x to string

  • The value is automatically converted to a number

Question 5

How do you explicitly declare a function’s return type in TypeScript?

  • function add(a: number, b: number) -> number {}

  • function add(a: number, b: number): number {}

  • function add(a: number, b: number) = number {}

  • function add(a: number, b: number) returns number {}

Question 6

What will be the inferred return type of this function?

TypeScript
function greet() {
  return "Hello, TypeScript!";
}


  • void

  • any

  • string

  • unknown

Question 7

What will be the inferred type of the variable flag in the following code?

TypeScript
let flag = true;


  • boolean

  • any

  • true

  • unknown

Question 8

Which of the following types is inferred when a variable is declared without assignment?

TypeScript
let value;


  • any

  • undefined

  • unknown

  • void

Question 9

How can you explicitly define a variable with multiple possible types?

  • let value: multiple<number, string> = 42;

  • let value = number | string = 42;

  • let value: number | string = 42;

  • let value: any = 42 | "hello";

Question 10

What happens when you use strictNullChecks in TypeScript?

  • null and undefined are treated as normal values

  • null and undefined cannot be assigned to variables of other types unless explicitly allowed

  • TypeScript ignores null values in type checking

  • null and undefined behave like numbers

There are 10 questions to complete.

Take a part in the ongoing discussion