All featured quizzes

TypeScript - Basics

Optional properties, strict mode, type narrowing, access modifiers, and tsconfig configuration.

Sample Questions

A preview of what's in this quiz. Answer them interactively for instant scoring and full explanations.

Which of the following is the correct file extension for standard TypeScript files?

1. Which of the following is the correct file extension for standard TypeScript files?

  • A..typescript
  • B..tscript
  • C..ts
  • D..tsx-only
What is the difference between `any` and `unknown` in TypeScript?

2. What is the difference between `any` and `unknown` in TypeScript?

  • A.any requires type checking; unknown does not.
  • B.unknown only accepts string or number; any accepts all types.
  • C.unknown requires type checking/assertion before performing operations; any does not.
  • D.There is no difference; they are aliases.
What is TypeScript?

3. What is TypeScript?

  • A.A complete replacement for JavaScript that runs directly in all browsers
  • B.A database query language for web applications
  • C.A typed superset of JavaScript that compiles to plain JavaScript
  • D.A styling library that extends CSS functionalities
What does the `void` type indicate when used as a function return type?

4. What does the `void` type indicate when used as a function return type?

  • A.The function returns null only.
  • B.The function can return any type.
  • C.The function does not return any value.
  • D.The function will never complete or terminate.
What does the `never` type represent in TypeScript?

5. What does the `never` type represent in TypeScript?

  • A.Null and undefined values
  • B.Variables that are declared but not initialized
  • C.Values that will never occur (e.g. from functions that throw errors)
  • D.Variables that can change their types dynamically
What is the standard configuration file used to configure a TypeScript project?

6. What is the standard configuration file used to configure a TypeScript project?

  • A.typescript.config
  • B.ts-config.xml
  • C.tsconfig.json
  • D.tsconfig.js
What does the `any` type represent in TypeScript?

7. What does the `any` type represent in TypeScript?

  • A.A type representing only empty values like null and undefined
  • B.A placeholder type representing only arrays and objects
  • C.A type that disables type checking, allowing any type of value
  • D.A union type of string and number only
How do you specify a type annotation for a variable `age` as a number in TypeScript?

8. How do you specify a type annotation for a variable `age` as a number in TypeScript?

  • A.let age = number;
  • B.let number age;
  • C.let age: number;
  • D.let age: Int;
Which command is used to run the TypeScript compiler and compile files?

9. Which command is used to run the TypeScript compiler and compile files?

  • A.typescript-compile
  • B.ts-run
  • C.tsc
  • D.compile-ts
How do you define a Union Type that allows a variable `id` to be either a string or a number?

10. How do you define a Union Type that allows a variable `id` to be either a string or a number?

  • A.let id: string & number;
  • B.let id: string or number;
  • C.let id: string | number;
  • D.let id: [string, number];
Which keyword is used to prevent a property of an interface or class from being reassigned after initialization?

11. Which keyword is used to prevent a property of an interface or class from being reassigned after initialization?

  • A.const
  • B.private
  • C.readonly
  • D.static
How do you annotate a parameter `name` as a string in a TypeScript function declaration?

12. How do you annotate a parameter `name` as a string in a TypeScript function declaration?

  • A.function greet(string name) { }
  • B.function greet(name = string) { }
  • C.function greet(name: string) { }
  • D.function greet(:string name) { }
How do you declare an optional parameter `email` in a function signature in TypeScript?

13. How do you declare an optional parameter `email` in a function signature in TypeScript?

  • A.function registerUser(name: string, optional email: string) { }
  • B.function registerUser(name: string, email: string?) { }
  • C.function registerUser(name: string, email?: string) { }
  • D.function registerUser(name: string, email: string = optional) { }
Which keyword is used to define a structural contract/shape of an object in TypeScript?

14. Which keyword is used to define a structural contract/shape of an object in TypeScript?

  • A.typealias
  • B.contract
  • C.interface
  • D.struct
How do you declare a default value for a parameter `role` in a TypeScript function?

15. How do you declare a default value for a parameter `role` in a TypeScript function?

  • A.function createUser(name: string, role?: string "member") { }
  • B.function createUser(name: string, role: string default "member") { }
  • C.function createUser(name: string, role: string = "member") { }
  • D.function createUser(name: string, role: string("member")) { }
What is a major difference between Interface and Type Alias in TypeScript?

16. What is a major difference between Interface and Type Alias in TypeScript?

  • A.Type aliases support inheritance using 'extends'; interfaces do not.
  • B.Interfaces can represent union types; type aliases cannot.
  • C.Interfaces can participate in declaration merging; type aliases cannot.
  • D.Interfaces compile to JavaScript classes; type aliases are removed.
Which keyword is used to declare a Type Alias in TypeScript?

17. Which keyword is used to declare a Type Alias in TypeScript?

  • A.interface
  • B.alias
  • C.type
  • D.define
Which operator is used to combine multiple types into one (Intersection Type)?

18. Which operator is used to combine multiple types into one (Intersection Type)?

  • A.|
  • B.&&
  • C.&
  • D.+
How do you define the return type of a function as a boolean in TypeScript?

19. How do you define the return type of a function as a boolean in TypeScript?

  • A.function isEven(n: number: boolean) { return true; }
  • B.function isEven(n: number) return boolean { return true; }
  • C.function isEven(n: number): boolean { return true; }
  • D.function boolean isEven(n: number) { return true; }
How do you mark a property `middleName` as optional within an interface?

20. How do you mark a property `middleName` as optional within an interface?

  • A.interface User { optional middleName: string; }
  • B.interface User { middleName: string | null; }
  • C.interface User { middleName?: string; }
  • D.interface User { middleName: string?; }

Looking for a different topic? Browse all subjects