diff --git a/client/src/pages/Notes/TypeScriptEssentials/TsTopics/TsBasics/BasicTypes.jsx b/client/src/pages/Notes/TypeScriptEssentials/TsTopics/TsBasics/BasicTypes.jsx new file mode 100644 index 0000000..c1d1fe2 --- /dev/null +++ b/client/src/pages/Notes/TypeScriptEssentials/TsTopics/TsBasics/BasicTypes.jsx @@ -0,0 +1,423 @@ +import React from 'react'; +import CodeBlock from '../../../components/CodeBlock'; + +const BasicTypes = () => { + return ( +
+ Basic types are the fundamental data types that TypeScript supports. + They provide type safety for the most common JavaScript values and operations. +
++ These are the basic building blocks of TypeScript's type system, corresponding to JavaScript's primitive values. +
+ +any when possibleunknown over any for dynamic valuesstring | null for optional string valuesnumber | undefined for optional numbersboolean for flags and togglesvoid for functions that don't return valuesnever for functions that never return+ Remember: Basic types are the foundation of TypeScript's type system. + They provide type safety for the most common JavaScript values and help catch errors early in development. +
++ Type annotations are explicit type declarations that tell TypeScript what type a variable, + function parameter, or return value should be. They help catch errors and provide better IDE support. +
++ You can add type annotations to variables using a colon followed by the type. +
+ +string[] not any[]any type unless absolutely necessarylet x: number = 5 (inference is enough)+ Use when: Type is not obvious or you want to be explicit +
++ Use when: Type is obvious from the value +
++ Tip: Start with type inference and add explicit annotations only when necessary. + TypeScript's inference is very good and reduces code verbosity while maintaining type safety. +
++ Type assertions tell the TypeScript compiler to treat a value as a specific type. + They don't change the runtime value - they only affect type checking at compile time. +
++ Type assertions are a way to tell TypeScript "I know more about this type than you do." + They're useful when you have a value that TypeScript can't properly infer the type of, + but you know what type it should be. +
+ ++ Important: Type assertions don't perform any runtime checking or conversion. + They only affect TypeScript's type checking. Use them carefully! +
++ Benefits: Runtime safety, better error handling +
++ Risks: No runtime safety, potential errors +
++ Remember: Type assertions are a powerful tool, but they should be used carefully. + They don't provide runtime safety, so always consider whether a type guard or proper type checking would be more appropriate. +
++ Type inference is TypeScript's ability to automatically determine the type of a variable, + function parameter, or return value based on the context and usage. It reduces the need for explicit type annotations. +
++ TypeScript analyzes your code and automatically assigns types based on: +
+ ++ Benefits: Less verbose, easier to maintain, type safety preserved +
++ Benefits: More explicit, better documentation, clearer intent +
+any to bypass inference+ Tip: Type inference is one of TypeScript's most powerful features. + It provides type safety with minimal code, but don't hesitate to add explicit types when they make your code clearer or more maintainable. +
++ TypeScript is a programming language that extends JavaScript by adding static type definitions. + It's designed for the development of large applications and transcompiles to JavaScript. +
++ TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. + It's developed and maintained by Microsoft and is open source. +
+ ++ Types are checked at compile time, not runtime. This catches errors before your code runs. +
++ All valid JavaScript code is also valid TypeScript code. +
++ TypeScript compiles to JavaScript, which runs in any JavaScript environment. +
++ You can add types gradually to existing JavaScript projects. +
+| Aspect | +JavaScript | +TypeScript | +
|---|---|---|
| Type System | +Dynamic | +Static | +
| Error Detection | +Runtime | +Compile Time | +
| IDE Support | +Basic | +Advanced | +
| Refactoring | +Manual | +Automated | +
| Learning Curve | +Gentle | +Moderate | +
+ TypeScript code is compiled to JavaScript. The TypeScript compiler (tsc) performs type checking + and then generates JavaScript code that can run in any JavaScript environment. +
+ ++ Remember: TypeScript is a tool that helps you write better JavaScript. + It doesn't replace JavaScript - it enhances it with type safety and better developer experience. +
+