This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { ResultAsync } from 'neverthrow'; | |
| type Success<T> = { data: T; error: null }; | |
| type Failure<E> = { data: null; error: E }; | |
| type Result<T, E = Error> = Success<T> | Failure<E>; | |
| export async function tryCatch<T, E = Error>( | |
| promise: Promise<T>, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useState, Activity } from "react"; | |
| export default function App() { | |
| const [show, setShow] = useState(true); | |
| return ( | |
| <> | |
| <button onClick={() => setShow(prev => !prev)}> | |
| Toggle | |
| </button> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Case 1 | |
| type ButtonConfig = { | |
| variant: 'primary' | 'secondary'; | |
| size: 'small' | 'default'; | |
| }; | |
| const button1: ButtonConfig = { | |
| variant: 'primary', | |
| size: 'small', | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type User = { | |
| name: 'psawn'; | |
| }; | |
| interface LoadingState { | |
| status: 'loading'; | |
| } | |
| interface SuccessState { | |
| status: 'success'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const req = { | |
| method: 'GET', | |
| url: 'api', | |
| } as const; | |
| // ^ "as const" converts fields from 'string' to literal types (e.g., exactly 'GET'). | |
| // It also makes the object read-only, preventing changes. | |
| function makeReq(url: string, method: 'GET' | 'POST') { | |
| return ''; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------- | |
| // 1. Custom sleep using Promise | |
| // ----------------------------- | |
| async function testCustomSleep() { | |
| const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | |
| console.log("Sleeping (custom Promise)..."); | |
| await sleep(1000); | |
| console.log("Done (custom Promise)!"); | |
| } |