A way to help typescript narrow your types is by explicitly adding a tag to them. This helps in narrowing the type when necessary.
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 debounce(func, wait){ | |
let timer; | |
let debouncedFunc = function(...args){ | |
clearTimeout(timer) | |
timer = setTimeout(() => { | |
func.apply(this, args) | |
}, wait) | |
} |
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
//conditionally add properties in a typesafe way | |
declare let hasMiddle: boolean | |
const fullName = { first: 'Kehinde', last: 'Adeleke'} | |
//the issue | |
const developer2 = {...fullName} | |
//if you tried adding a new key, | |
//typescript would scream that middle does not exist on type { first: string, last: string} |
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 _cache: {[url: string]:string} = {} | |
async function fetchWithCache(url:string){ | |
if(url in cache){ | |
return _cache[url] | |
} | |
const response = await fetch(url) | |
const text = response.text() | |
_cache[url] = text |
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
.backdrop { | |
position: absolute; | |
top: 0; | |
left: 0; | |
height: 100%; | |
width: 100%; | |
background: #000000e1; | |
display: flex; | |
align-items: center; | |
justify-content: center; |
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
<form | |
action="/api/search" | |
method="post" | |
onSubmit={event => { | |
event.preventDefault(); | |
fetch( | |
'/api/search', | |
{ | |
method: 'POST', |
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 Grid from './Grid' | |
function App(){ | |
return ( | |
<Grid numRows={2} numCols={4}/> | |
) | |
} | |
export default App |
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, useEffect } from "react"; | |
const useLocalStorage = (key: string, initialValue: any) => { | |
const isServer = typeof window === "undefined"; | |
const [value, setValue] = useState(() => { | |
if (isServer) { | |
return initialValue; | |
} |
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 cons = (x, y) => (m) => m(x, y) | |
const car = (z) => z((p, q) => p) | |
const cdr = (z) => z((p, q) => q) | |
const someLinkedList = cons(1, cons(2, cons(3 , null))) | |
// iterating |
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 * as esbuild from 'esbuild-wasm' | |
import axios from 'axios' | |
export const unpkgPathPlugin = () => { | |
return { | |
name: 'unpkg-path-plugin', | |
setup(build: esbuild.PluginBuild) { | |
build.onResolve({ filter: /.*/ }, async (args) => { | |
console.log('onResolve', args) | |
if(args.path === 'index.js'){ |