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
| document.querySelector('#myButton').addEventListener('click', e => { | |
| e.currentTarget; // Type is EventTarget | |
| const button = e.currentTarget as HTMLButtonElement; // buttonType is HTMLButtonElement | |
| }); |
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 Person = { name: string }; | |
| const john: Person = { | |
| name: 'John', | |
| dob: '05/09/1982' | |
| // Object literal may only specify known properties and | |
| // 'dob' does not exist in type 'Person' | |
| }; | |
| const jane = { |
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 john: Person = {}; // Error | |
| const jane = {} as Person; // No error |
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 Person = { name: string }; | |
| const john: Person = { name: 'John Doe' }; // Type declaration | |
| const jane = { name: 'Jane Doe' } as Person; // Type assertion |
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 promises = await settledPromises; | |
| promises; // [{ status: '...', value: '...' }, ...] |
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
| async function run() { | |
| const settledPromises = Promise.allSettled([ | |
| new Promise((r, reject) => setTimeout(() => reject("Out of stock"), delay)), | |
| new Promise((r, reject) => setTimeout(() => reject("Out of stock"), delay)) | |
| ]); | |
| // wait... | |
| const promises = await settledPromises; | |
| // after 0.8 seconds |
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
| async function run() { | |
| const settledPromises = Promise.allSettled([ | |
| new Promise((resolve) => setTimeout(() => resolve(["shirts", "trousers"]), 800)), | |
| new Promise((r, reject) => setTimeout(() => reject("Out of stock"), delay)) | |
| ]); | |
| // wait... | |
| const promises = await settledPromises; | |
| // after 0.8 seconds |
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
| async function run() { | |
| const settledPromises = Promise.allSettled([ | |
| new Promise((resolve) => setTimeout(() => resolve(["shirts", "trousers"]), 800)), | |
| new Promise((resolve) => setTimeout(() => resolve(["sneakers", "boots"]), 800)), | |
| ]); | |
| // wait... | |
| const promises = await settledPromises; | |
| // after 0.8 seconds |
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 { useMemo, useState } from "react"; | |
| function Child({ total }) { | |
| return <p>{`Total: ${total}`}</p>; | |
| } | |
| export default function Parent() { | |
| const [a, setA] = useState(0); | |
| const [b, setB] = useState(0); | |
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
| function calc(x, y) { | |
| return x + y; | |
| } | |
| const memoizedValue = useMemo(() => { | |
| return calc(a, b); | |
| }, [a, b]); |