🔷

TypeScript: Types, Interfaces & Generics

77 cards · 🇺🇸 English

Core TypeScript type system: types, interfaces, and generics.

Study all 77 cards for free

Flashcards, quizzes, pixel-art games and a daily review that remembers for you.

Study this deck free

Try the first 10 cards

1 / 10
Question

Three primitive types in TypeScript

Knew itLearning

Tap the card for the answer · swipe right if you knew it, left if not

Already have an account? Sign in

Cards

  1. 1Three primitive types in TypeScript
    string, number, and boolean
  2. 2Why should you use lowercase type names (string, number, boolean)?
    The capitalized versions (String, Number, Boolean) refer to special built-in types that rarely appear in your code
  3. 3Specify the type of an array in TypeScript
    Use number[] or Array<number> syntax (both are equivalent)
  4. 4'any' type in TypeScript
    A special type that disables all type checking - you can access any properties, call it as a function, or assign it to any type
  5. 5What compiler flag prevents implicit 'any' types?
    noImplicitAny
  6. 6Where do type annotations go in TypeScript?
    After the thing being typed (not before like int x = 0 in other languages)
  7. 7Type inference
    TypeScript automatically determining types based on context, like inferring a variable's type from its initializer
  8. 8Add a type annotation to a function parameter
    After the parameter name: function greet(name: string) { ... }
  9. 9Specify a function's return type
    After the parameter list: function getFavoriteNumber(): number { ... }
  10. 10Contextual typing
    When TypeScript infers parameter types based on how and where a function is used (e.g., in array.forEach)
  11. 11Define an object type inline
    List properties and types: function printCoord(pt: { x: number; y: number }) { ... }
  12. 12Make an object property optional
    Add a ? after the property name: { first: string; last?: string }
  13. 13What happens when you access an optional property?
    You get undefined if it doesn't exist, so you must check for undefined before using it
  14. 14Union type
    A type formed from two or more other types, representing values that may be any one of those types
  15. 15Define a union type
    Use the pipe operator: number | string
  16. 16Narrowing
    When TypeScript deduces a more specific type for a value based on the structure of the code (e.g., after a typeof check)
  17. 17How can you narrow a union type?
    Use typeof checks, Array.isArray(), or other runtime checks to determine which type it is
  18. 18Type alias
    A name for any type, defined with: type Point = { x: number; y: number }
  19. 19Can type aliases be used for union types?
    Yes: type ID = number | string
  20. 20Interface in TypeScript
    A way to name an object type with its properties and their types
  21. 21Main difference between interfaces and type aliases
    Interfaces can be re-opened to add new properties (declaration merging), type aliases cannot
  22. 22Extend an interface
    interface Bear extends Animal { honey: boolean }
  23. 23Extend a type alias
    Use intersection: type Bear = Animal & { honey: boolean }
  24. 24Can you reopen a type alias to add properties?
    No, a type cannot be changed after being created
  25. 25Type assertion
    A way to tell TypeScript a more specific type than it knows: document.getElementById('canvas') as HTMLCanvasElement
  26. 26Alternative syntax for type assertions
    Angle brackets: <HTMLCanvasElement>document.getElementById('canvas') (not usable in .tsx files)
  27. 27Do type assertions affect runtime behavior?
    No, they are removed by the compiler and have no runtime impact
  28. 28Literal type
    A type that refers to a specific string or number value, like 'hello' or 42
  29. 29How are const variables typed vs let variables?
    const gets a literal type (const x = 'hello' → type 'hello'), let gets a general type (let x = 'hello' → type string)
  30. 30How can you use literal types with unions?
    To restrict values to a specific set: alignment: 'left' | 'right' | 'center'

And 47 more cards. Study the whole deck free

Articles about this deck

More Programming decks

See all
Study all 77 cards free