- Time complexity:
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 Email = string & { __brand: "Email" }; | |
const email = "[email protected]"; | |
function isValidEmail(input: string): input is Email { | |
return /@/.test(input); | |
} | |
function sendEmail(email: Email) { | |
console.log(email); | |
// send email |
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 RgbTuple = Tuple<3, number>; | |
// ^? type RgbTuple = readonly [number, number, number] | |
const red: RgbTuple = [255, 0, 0]; | |
type Tuple<Length, Type, Acc extends Type[] = []> = Acc["length"] extends Length | |
? Readonly<Acc> | |
: Tuple<Length, Type, [...Acc, Type]>; |
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 add(a: number, b: number): number { | |
return a + b; | |
} | |
type Test = typeof add; | |
// ^ = type Test = (a: number, b: number) => number | |
type Test2 = ReturnType<typeof add>; | |
// ^ = type Test2 = number | |
// |
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 Locations = 'Zurich' | 'Warsaw' | 'London'; | |
function getCountryLocation(location: Locations) { | |
switch (location) { | |
case 'Zurich': | |
return 'Switzerland'; | |
case 'Warsaw': | |
return 'Poland'; | |
case 'London': | |
return 'UK'; |
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 GapType = 'margin' | 'padding'; | |
type PositionType = 'top' | 'right' | 'bottom' | 'left'; | |
type GapCss = `${GapType}-${PositionType}`; | |
// GapCss: "margin-top" | "margin-right" | "margin-bottom" | "margin-left" | "padding-top" | "padding-right" | "padding-bottom" | "padding-left" | |
// | |
type SizeType = 'rem' | 'em' | 'px' | '%' | 'vh' | 'vw' | ''; | |
type SizeCss = `${number}${SizeType}`; |
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 Events = { | |
add: string; | |
remove: string; | |
move: string; | |
}; | |
type EventKeys = keyof Events; | |
const userActions: OnEvent = { | |
onAdd: () => {}, |
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 { OtherComponent } from "./other-component"; | |
import React, { useRef, ElementRef } from "react"; | |
// Pass it in via typeof! | |
type OtherComponentRef = ElementRef<typeof OtherComponent>; | |
const Component = () => { | |
const ref = useRef<OtherComponentRef>(null); | |
return <OtherComponent ref={ref}>Hello</OtherComponent>; | |
}; |
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 React, { forwardRef } from "react"; | |
// Declare a type that works with generic components | |
type FixedForwardRef = <T, P = {}>( | |
render: (props: P, ref: React.Ref<T>) => React.ReactElement | |
) => (props: P & React.RefAttributes<T>) => React.ReactElement; | |
// Cast the old `forwardRef` to the new one | |
export const fixedForwardRef = forwardRef as FixedForwardRef; |
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
# Sliding window algorithm | |
# | |
# @param {String} s | |
# @return {Integer} | |
def length_of_longest_substring(s) | |
max = 0 | |
subset = [] | |
s.each_char do |char| | |
if subset.include?(char) |
NewerOlder