Types Quiz - Languages TypeScript
Free practice quiz: Types (Languages TypeScript). 10 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
This Languages TypeScript practice quiz has 10 multiple-choice questions on Types, and 10 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.
Start quiz: 10 questions, freeSample questions
In TypeScript, which of the following keywords is used to define a type alias for a primitive type?
Show answer
Answer: B. type
The `type` keyword in TypeScript is used to create a type alias, which can be a primitive type (like `string`, `number`, `boolean`), an object type, a union type, etc. `interface` is primarily for object shapes, `class` for object blueprints, and `enum` for named numeric constants.
What is the primary advantage of using TypeScript's type system over plain JavaScript?
Show answer
Answer: C. Early detection of errors
TypeScript's static type checking allows many common programming errors (like type mismatches) to be caught during development (compile-time) rather than at runtime, leading to more robust code. While it can indirectly help with performance and refactoring, its core benefit is error prevention.
What does the `readonly` modifier in TypeScript signify when applied to a property of an object type?
Show answer
Answer: B. The property's value cannot be changed after initialization.
The `readonly` modifier ensures that a property's value is immutable once it has been assigned. This means it cannot be reassigned after the object is created.
Which type in TypeScript represents a value that can never occur?
Show answer
Answer: C. never
The `never` type represents the type of values that never occur. This typically includes functions that always throw an error or functions that never return.
If you have an object type with optional properties, how are those properties indicated in TypeScript?
Show answer
Answer: B. By appending `?` to the property name.
The `?` symbol after a property name in an interface or type alias signifies that the property is optional, meaning it may or may not be present on an object.
More questions in this quiz
Answers and explanations are in the full quiz.
- Consider the following TypeScript code: `let greeting: string | null = 'Hello';`. What kind of type is `string | null`?
- Which of the following TypeScript types represents a function that takes two numbers and returns a number?
- Consider an array declared as `let numbers: number[] = [1, 2, 3];`. What is the type of `numbers`?
- What is the purpose of the `enum` type in TypeScript?
- What is the type of the `arguments` object within a traditional JavaScript function (not an arrow function) when used in TypeScript?