Skip to content

Instantly share code, notes, and snippets.

View szaranger's full-sized avatar

Sean Amarasinghe szaranger

View GitHub Profile
document.querySelector('#myButton').addEventListener('click', e => {
e.currentTarget; // Type is EventTarget
const button = e.currentTarget as HTMLButtonElement; // buttonType is HTMLButtonElement
});
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 = {
const john: Person = {}; // Error
const jane = {} as Person; // No error
type Person = { name: string };
const john: Person = { name: 'John Doe' }; // Type declaration
const jane = { name: 'Jane Doe' } as Person; // Type assertion
const promises = await settledPromises;
promises; // [{ status: '...', value: '...' }, ...]
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
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
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
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);
function calc(x, y) {
return x + y;
}
const memoizedValue = useMemo(() => {
return calc(a, b);
}, [a, b]);