Any2Quiz
Computer Science / Languages / TypeScript

Types Quiz - Languages TypeScript

Free practice quiz: Types (Languages TypeScript). 10 multiple-choice questions with answers and explanations. Play online, no sign-up needed.

Play all 10 questions free

Sample questions

  1. In TypeScript, which of the following keywords is used to define a type alias for a primitive type?

    1. interface
    2. type
    3. class
    4. enum
    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.

  2. What is the primary advantage of using TypeScript's type system over plain JavaScript?

    1. Improved runtime performance
    2. Reduced boilerplate code
    3. Early detection of errors
    4. Automatic code refactoring
    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.

  3. What does the `readonly` modifier in TypeScript signify when applied to a property of an object type?

    1. The property's value can be changed once during initialization.
    2. The property's value cannot be changed after initialization.
    3. The property is only accessible from within the class.
    4. The property will be removed after compilation.
    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.

  4. Which type in TypeScript represents a value that can never occur?

    1. null
    2. undefined
    3. never
    4. void
    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.

  5. If you have an object type with optional properties, how are those properties indicated in TypeScript?

    1. By prefixing the property name with `optional`.
    2. By appending `?` to the property name.
    3. By enclosing the property name in parentheses.
    4. Optional properties are not directly supported 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.

Take the full quiz

More quizzes

Browse all TypeScript quizzes