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
| // Before: Multiple useState = multiple potential renders | |
| const [user, setUser] = useState<User | null>(null) | |
| const [loading, setLoading] = useState(true) | |
| const [error, setError] = useState<Error | null>(null) | |
| // Async callback triggers 3 separate updates | |
| setLoading(true) | |
| const userData = await api.getUser(userId) | |
| setUser(userData) | |
| setLoading(false) |
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
| // After: Single useImmer = guaranteed single render per batch | |
| import { useImmer } from 'use-immer' | |
| const [state, updateState] = useImmer({ | |
| user: null as User | null, | |
| loading: true, | |
| error: null as Error | null, | |
| }) | |
| // All updates batched into ONE render |
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
| // Before: Callback hell | |
| function fetchUserData(userId: string, callback: (err: Error | null, data?: UserData) => void) { | |
| db.getUser(userId, (err, user) => { | |
| if (err) return callback(err) | |
| api.fetchPosts(user.id, (err, posts) => { | |
| if (err) return callback(err) | |
| api.fetchComments(posts[0].id, (err, comments) => { | |
| if (err) return callback(err) |
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
| // Before: Callback hell | |
| function fetchUserData(userId: string, callback: (err: Error | null, data?: UserData) => void) { | |
| db.getUser(userId, (err, user) => { | |
| if (err) return callback(err) | |
| api.fetchPosts(user.id, (err, posts) => { | |
| if (err) return callback(err) | |
| api.fetchComments(posts[0].id, (err, comments) => { | |
| if (err) return callback(err) |
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
| // JavaScript - AI has no idea what 'user' contains | |
| function authenticateUser(user, password) { | |
| // AI suggests generic code because it can only guess | |
| if (user && password) { | |
| return validatePassword(user.password, password) | |
| } | |
| } |
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 type { Options } from 'tsup' | |
| const config: Options = { | |
| // | |
| } | |
| export default config |
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 config: Options = { | |
| entry: ['src/index.ts'], | |
| dts: true, | |
| sourcemap: true, | |
| format: ['iife', 'cjs', 'esm'], | |
| } |
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
| export function sayHello() { | |
| console.log('hello') | |
| } |
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
| mkdir my-typescript-library | |
| cd my-typescript-library |
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 config: Options = { | |
| entry: ['src/index.ts'], | |
| dts: true, | |
| sourcemap: true, | |
| } |