TypeScript Flashcards
What is TypeScript?
TypeScript is a superset of JavaScript that adds static typing.
Static Typing
TypeScript allows you to define variable types, making code more predictable and easier to debug.
Transpilation
TypeScript code is compiled into JavaScript for execution in browsers or Node.js.
TypeScript File Extension
TypeScript files use the .ts extension.
Benefits of TypeScript
Improves code quality, better error detection, supports modern JS features.
Interfaces
Used to define contracts for objects, specifying their structure.
Classes in TypeScript
Allow for creating objects with defined properties and methods, supporting OOP principles.
Enums
Define a set of named constants, making code more readable.
Generics
Enable writing reusable and type-safe code components.
Union Types
Allow variables to hold more than one type, e.g., string | number.
Type Aliases
Provide a way to name and reuse custom types.
Literal Types
Restrict a variable to specific values, e.g., let direction: ‘up’ | ‘down’.
Type Inference
TypeScript can infer variable types based on assigned values.
Any Type
A type that disables type checking, useful in dynamic code.
Never Type
Indicates values that never occur, e.g., in unreachable code.
Void Type
Used for functions that don’t return a value.
Tuple
An array with fixed types and length.
Type Assertion
Tell the compiler to treat a value as a specific type using as or <>.
Optional Parameters
Functions can define optional parameters with the ? syntax.
Readonly Modifier
Prevents reassignment of properties or variables.
Access Modifiers
Keywords like public, private, and protected control property access.
Default Parameters
Functions can have parameters with default values.
Decorators
Special annotations used to modify classes or methods at runtime.
Namespaces
Used to organize and group related code.
Modules
Allow splitting code into reusable files and importing/exporting between them.
TypeScript Compiler
The tsc command compiles TypeScript to JavaScript.
Configuration File
tsconfig.json configures TypeScript compiler options.
Strict Mode
Enabled in tsconfig.json to enforce strict typing rules.
Declaration Files
Files with a .d.ts extension provide type definitions for JavaScript libraries.
Mapped Types
Transform properties in a type, e.g., making them optional.
Intersection Types
Combine multiple types into one using &.
Keyof Operator
Get all keys of a type as a union.
Indexed Access Types
Access the type of a property in an object type.
Utility Types
Predefined types like Partial, Readonly, Record, etc., for common transformations.
Type Guards
Runtime checks to ensure a variable matches a specific type.
Abstract Classes
Classes that cannot be instantiated but can be extended.
Promises in TypeScript
Handle asynchronous operations with typed Promises.
Error Handling
Use try-catch blocks with type-safe error objects.
Compatibility with JS
TypeScript is fully compatible with existing JavaScript code.
DOM Manipulation
Type-safe interaction with HTML elements using TypeScript.
Function Overloading
Define multiple function signatures for one implementation.
Constructor
Special method in classes for initializing objects.
Static Members
Class-level properties or methods shared across all instances.
TypeScript Playground
Online editor to experiment with TypeScript code.
Tooling Support
Integrated into many IDEs like VS Code for autocompletion and type checking.
Code Refactoring
Simplified and safer refactoring with static type checking.
Community Support
Growing community and resources for TypeScript developers.
Migration
Easily migrate JavaScript projects incrementally to TypeScript.