Welcome to the TypeScript Snippets repository! This repository is designed to provide you with bite-sized examples of various TypeScript features, from the basics to advanced concepts. Whether you're new to TypeScript or looking to deepen your understanding, these snippets are here to guide you.
Before diving into the snippets, ensure you have the necessary tools installed:
-
Node.js: TypeScript is a superset of JavaScript, and we'll use Node.js to run our compiled TypeScript files. Download Node.js
-
TypeScript: Install TypeScript globally using npm (Node.js package manager):
npm install -g typescript
-
Clone the Repository:
git clone https://github.com/your-username/ts-snippets.git cd ts-snippets
-
Install Dependencies:
npm install
-
Navigating the Snippets: Each
.ts
file corresponds to a specific TypeScript concept. The file name gives a hint about the topic it covers. -
Running a Snippet:
- First, compile the TypeScript file:
tsc <filename>.ts
- Then, run the compiled JavaScript file:
node lib/<filename>.js
- First, compile the TypeScript file:
-
Learning Process:
- Read the Comments: Each snippet is accompanied by comments explaining the code. Start by reading these comments to understand the concept.
- Experiment: Don't just read; experiment! Modify the code, see how TypeScript reacts, and learn from any errors or warnings.
- Practice: After understanding a concept, try to recreate it without looking at the snippet. This will reinforce your learning.
- Initialization
- Primitive Types
- Instance Types
- Arrays
- Tuples
- Object Types
- Const Declarations
- Functions
- Structural Typing
- Classes
- Target Compiler Option
- Generics
- Special Types
- JavaScript to TypeScript
- Universal Utilities
- Type Assertions
- Type Casting
- Modules
- Type Declarations
- Async Await
- TypeScript Node
- Lexical This
- Read-Only Modifier
- Union Types
- Literal Types
- Type Narrowing
- Discriminated Unions
- Class Parameter Properties
- Null vs Undefined
- Intersection Types
- Optional Modifier
- Non-Null Assertion Operator
- Interfaces
- Interface Declaration Merging
- Types vs Interfaces
- Never Type
- Implements Keyword
- Definite Assignment Assertion
- User Defined Type Guards
- Assertion Functions
- Function Overloading
- Call Signatures
- Abstract Class
- Index Signatures
- Readonly Arrays
- Double Assertion
- Const Assertion
- This Parameter
- Generic Constraints
- Temporal Uncertainty
- Typeof Operator
- Lookup Types
- Keyof Type Operator
- Conditional Types
- Conditional Types with Unions and Never
- Infer Keyword and Return Type
- Mapped Types
- Mapped Type Modifiers
- Template Literal Type
- Partial T
- Required T
- Record
- Auto Complete Literal Unions With Primitives
- Undefined vs Optional
- Satisfies Operator
- Property Key
- This Type Utility
- Awaited Utility
- String Utilities
Feel free to contribute to this repository by adding new snippets or enhancing existing ones. Ensure your contributions are well-commented to maintain the educational purpose of this repository.
This project is licensed under the MIT License. See the LICENSE file for details.
Happy Learning! 🎉