diff --git a/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/Enums.jsx b/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/Enums.jsx new file mode 100644 index 0000000..438a913 --- /dev/null +++ b/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/Enums.jsx @@ -0,0 +1,53 @@ +import React from 'react'; +import CodeBlock from '../../../components/CodeBlock'; + +const Enums = () => { + return ( +
+
+

Enums

+ +

+ Enums define a set of named constants. Prefer union string literals for + most cases, but enums can be helpful when interoperating with existing code or when you + need reverse mapping. +

+ + + +

Alternatives

+ + role === 'admin';`} + /> +
+
+ ); +}; + +export default Enums; + + diff --git a/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/Generics.jsx b/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/Generics.jsx new file mode 100644 index 0000000..23db413 --- /dev/null +++ b/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/Generics.jsx @@ -0,0 +1,52 @@ +import React from 'react'; +import CodeBlock from '../../../components/CodeBlock'; + +const Generics = () => { + return ( +
+
+

Generics

+ +

+ Generics enable reusable components that work with a variety of types while maintaining + type safety. +

+ + (value: T): T { + return value; +} + +const n = identity(42); +const s = identity('hello'); // type inferred + +// Generic interfaces +interface ApiResponse { + data: T; + status: number; +} + +type User = { id: number; name: string }; +const res: ApiResponse = { data: { id: 1, name: 'Alex' }, status: 200 };`} + /> + +

Constraints

+ + (obj: T, key: K): T[K] { + return obj[key]; +} + +const user = { id: 1, name: 'Alex' }; +const name = getProp(user, 'name'); // type: string`} + /> +
+
+ ); +}; + +export default Generics; + + diff --git a/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/IntersectionTypes.jsx b/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/IntersectionTypes.jsx new file mode 100644 index 0000000..097f769 --- /dev/null +++ b/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/IntersectionTypes.jsx @@ -0,0 +1,43 @@ +import React from 'react'; +import CodeBlock from '../../../components/CodeBlock'; + +const IntersectionTypes = () => { + return ( +
+
+

Intersection Types

+ +

+ Intersection types combine multiple types into one using the & operator. The + resulting type must satisfy all members. +

+ + + +

With Interfaces

+ + +
+
+ ); +}; + +export default IntersectionTypes; + + diff --git a/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/LiteralTypes.jsx b/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/LiteralTypes.jsx new file mode 100644 index 0000000..4673428 --- /dev/null +++ b/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/LiteralTypes.jsx @@ -0,0 +1,33 @@ +import React from 'react'; +import CodeBlock from '../../../components/CodeBlock'; + +const LiteralTypes = () => { + return ( +
+
+

Literal Types

+ +

+ Literal types restrict a value to a specific string, number, or boolean. + Combine them with unions to model finite sets. +

+ + +
+
+ ); +}; + +export default LiteralTypes; + + diff --git a/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/UnionTypes.jsx b/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/UnionTypes.jsx new file mode 100644 index 0000000..19e83af --- /dev/null +++ b/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/UnionTypes.jsx @@ -0,0 +1,56 @@ +import React from 'react'; +import CodeBlock from '../../../components/CodeBlock'; + +const UnionTypes = () => { + return ( +
+
+

Union Types

+ +

+ Union types let a value be one of several types using the | operator. They are + ideal when an API accepts a limited set of primitives or shapes. +

+ + + +

Unions with Objects

+ + +
+
+ ); +}; + +export default UnionTypes; + + diff --git a/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/UtilityTypes.jsx b/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/UtilityTypes.jsx new file mode 100644 index 0000000..5e4c7ce --- /dev/null +++ b/client/src/pages/Notes/TypeScriptEssentials/TsTopics/AdvancedTypes/UtilityTypes.jsx @@ -0,0 +1,53 @@ +import React from 'react'; +import CodeBlock from '../../../components/CodeBlock'; + +const UtilityTypes = () => { + return ( +
+
+

Utility Types

+ +

+ TypeScript includes built-in utility types that transform existing types to help with + common tasks like making properties optional, required, or read-only. +

+ + ; + +// Required: make all props required +type UserRequired = Required; + +// Readonly: make all props readonly +type UserReadonly = Readonly; + +// Pick / Omit +type UserPublic = Pick; +type UserPrivate = Omit; + +// Record +type UsersById = Record;`} + /> + +

NonNullable & ReturnType

+ + ; // string + +function makeUser(name: string) { + return { id: 1, name }; +} +type MakeUserReturn = ReturnType;`} + /> +
+
+ ); +}; + +export default UtilityTypes; + + diff --git a/client/src/pages/Notes/TypeScriptEssentials/TypeScriptEssentials.jsx b/client/src/pages/Notes/TypeScriptEssentials/TypeScriptEssentials.jsx index acb40b7..d0ebcf1 100644 --- a/client/src/pages/Notes/TypeScriptEssentials/TypeScriptEssentials.jsx +++ b/client/src/pages/Notes/TypeScriptEssentials/TypeScriptEssentials.jsx @@ -23,12 +23,12 @@ import TypeInference from './TsTopics/TsBasics/TypeInference'; import TypeAssertions from './TsTopics/TsBasics/TypeAssertions'; // Advanced Types -// import UnionTypes from './TsTopics/AdvancedTypes/UnionTypes'; -// import IntersectionTypes from './TsTopics/AdvancedTypes/IntersectionTypes'; -// import LiteralTypes from './TsTopics/AdvancedTypes/LiteralTypes'; -// import Enums from './TsTopics/AdvancedTypes/Enums'; -// import Generics from './TsTopics/AdvancedTypes/Generics'; -// import UtilityTypes from './TsTopics/AdvancedTypes/UtilityTypes'; +import UnionTypes from './TsTopics/AdvancedTypes/UnionTypes'; +import IntersectionTypes from './TsTopics/AdvancedTypes/IntersectionTypes'; +import LiteralTypes from './TsTopics/AdvancedTypes/LiteralTypes'; +import Enums from './TsTopics/AdvancedTypes/Enums'; +import Generics from './TsTopics/AdvancedTypes/Generics'; +import UtilityTypes from './TsTopics/AdvancedTypes/UtilityTypes'; // Functions and Classes // import FunctionTypes from './TsTopics/FunctionsClasses/FunctionTypes'; @@ -118,13 +118,13 @@ const TypeScriptEssentials = () => { } /> } /> - {/* Advanced Types - Will be added in PR #4 */} - {/* } /> + {/* Advanced Types */} + } /> } /> } /> } /> } /> - } /> */} + } /> {/* Functions and Classes - Will be added in PR #5 */} {/* } />