TypeScript Basics: Variables and Types
8 Questions
0 Views

TypeScript Basics: Variables and Types

Created by
@AuthenticOboe

Questions and Answers

What keyword is used to create a new name for a type in TypeScript?

  • define
  • type (correct)
  • declare
  • interface
  • Which of the following is a correct syntax for declaring a tuple type?

  • let myTuple: number = ['one', 1]
  • let myTuple: (string, number[])
  • let myTuple: [string, number]
  • let myTuple: [number, string] = [1, 'one'] (correct)
  • Which statement about the export and import statements is true?

  • They define the structure of an object.
  • They group related code into one function.
  • They are used to declare variables.
  • They are used to separate code into different files. (correct)
  • What does the readonly modifier do when applied to an array in TypeScript?

    <p>It prevents any modification to the array after declaration.</p> Signup and view all the answers

    What is the purpose of a callback function in TypeScript?

    <p>To execute a function in response to another function.</p> Signup and view all the answers

    Which loop in TypeScript is specifically used to iterate over the keys of an object?

    <p>for...in</p> Signup and view all the answers

    When defining an enum, how would you assign string values to its members?

    <p>enum Colors { Red = 'RED', Green = 'GREEN' }</p> Signup and view all the answers

    How does explicit casting work in TypeScript?

    <p>It either uses <code>as</code> or angle brackets to assert types.</p> Signup and view all the answers

    Study Notes

    TypeScript Basics

    Variables and Types

    • Declaration: Use let, const, or var for variable declarations.
    • Types:
      • Primitive Types: number, string, boolean, null, undefined, symbol, bigint.
      • Array Types:
        • Syntax: number[] or Array<number>.
      • Tuple Types: Fixed-length array with specified types, e.g., [string, number].

    Interfaces and Types

    • Interfaces:
      • Define the shape of an object.
      • Can be extended with extends.
      • Supports optional properties using ?.
    • Type Aliases:
      • Create new names for types using the type keyword.
      • Example: type Point = { x: number; y: number; }.

    Functions and Callbacks

    • Function Declaration:
      • Syntax: function funcName(param: type): returnType {}.
    • Optional Parameters: Use ?.
    • Default Parameters: Define default values.
    • Callback Functions: Pass functions as arguments.
    • Function Types: Define types for function parameters and return values.

    Modules and Namespaces

    • Modules:
      • Separate code into files using export and import.
      • Can export variables, functions, and classes.
    • Namespaces:
      • Group related code.
      • Use the namespace keyword.
      • Useful for organizing code without module boundaries.

    Loops

    • For Loop: Standard loop structure.
    • For...of: Iterates over iterable objects (arrays, strings).
    • For...in: Iterates over object keys.
    • While Loop: Continues until a condition is false.

    Arrays

    • Declaration: let arr: number[] = [1, 2, 3];.
    • Array Methods:
      • push(), pop(), map(), filter(), reduce().
    • Readonly Arrays: Use readonly modifier to prevent modification.

    Enums

    • Enum Definition: Group related constants.
      • Syntax: enum Direction { Up, Down, Left, Right }.
    • String Enums: Assign string values to enum members.
      • Example: enum Colors { Red = 'RED', Green = 'GREEN' }.

    Explicit Casting

    • Casting Syntax: Use as or angle brackets for type assertion.
      • Example: let strLength = (someValue as string).length;.

    Type Aliasing

    • Creating Type Aliases: Use the type keyword to create custom types.
      • Example: type ID = string | number;.

    Structural Typing

    • Definition: Type compatibility based on the structure of the types.
    • Duck Typing: If it has the required properties, it is compatible.
    • Example: Interfaces can be compatible even if they have different names if their structures match.

    Variables and Types

    • Utilize let, const, or var for variable declarations to define mutable and immutable variables.
    • Primitive Types include:
      • number: Represents numeric values.
      • string: For text values.
      • boolean: Represents true/false values.
      • null and undefined: Represent empty or uninitialized values.
      • symbol: Unique and immutable values used as object keys.
      • bigint: Represents large integers beyond the number range.
    • Array Types are defined using:
      • Syntax: number[] for an array of numbers, or Array<number>.
    • Tuple Types allow specifying fixed-length arrays with designated types, e.g., a tuple can look like [string, number].

    Interfaces and Types

    • Interfaces serve to define the structure of objects and can be extended via the extends keyword.
    • They allow optional properties denoted by ? to specify attributes that may not be present.
    • Type Aliases allow creating custom names for types using the type keyword, such as type Point = { x: number; y: number; }.

    Functions and Callbacks

    • Function Declarations use the format: function funcName(param: type): returnType {} for defining functions.
    • Optional Parameters can be indicated using ? to make parameters non-mandatory.
    • Default Parameters allow specifying default values in case no arguments are passed.
    • Callback Functions enable passing functions as arguments to other functions.
    • Function Types specify the types of function parameters and their return values.

    Modules and Namespaces

    • Modules help segregate code into files using export and import keywords for better organization.
    • Modules can export multiple elements like variables, functions, and classes.
    • Namespaces are useful for grouping related code without the constraints of module boundaries. Use the namespace keyword for organization.

    Loops

    • For Loop is a standard iteration structure to execute a block of code multiple times.
    • For...of loop is designed to iterate over iterable objects, such as arrays and strings.
    • For...in loop iterates through keys of objects.
    • While Loop continues executing as long as a specified condition remains true.

    Arrays

    • Array Declaration can be done as: let arr: number[] = [1, 2, 3];.
    • Common Array Methods include:
      • push(): Adds an item to the end.
      • pop(): Removes the last item.
      • map(), filter(), reduce(): Higher-order functions for manipulating arrays.
    • Readonly Arrays can be defined using the readonly modifier to prevent any modifications.

    Enums

    • Enum Definition allows grouping related constants for better readability and structure, e.g., enum Direction { Up, Down, Left, Right }.
    • String Enums enable assigning string values to enum members, like enum Colors { Red = 'RED', Green = 'GREEN' }.

    Explicit Casting

    • Casting Syntax can utilize as or angle brackets for type assertion, such as let strLength = (someValue as string).length;.

    Type Aliasing

    • Creating Type Aliases can be achieved using the type keyword for simplifying type definitions, e.g., type ID = string | number;.

    Structural Typing

    • Definition: Compatible types are determined by their structure rather than their explicit definitions.
    • Duck Typing indicates a type is compatible if it has the necessary properties required by the context.
    • Example: Interfaces can be compatible across different naming if their properties align.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    This quiz covers the essential concepts of variables and types in TypeScript. You will learn about variable declarations, primitive types, array types, and tuple types. Test your understanding of interfaces and their role in defining object shapes.

    More Quizzes Like This

    Master TypeScript
    10 questions

    Master TypeScript

    AstoundedMahoganyObsidian avatar
    AstoundedMahoganyObsidian
    typescript quiz
    5 questions

    typescript quiz

    AstoundedMahoganyObsidian avatar
    AstoundedMahoganyObsidian
    TypeScript vs JavaScript: Key Differences
    14 questions
    Use Quizgecko on...
    Browser
    Browser