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
// Do these syntaxes produce the same result? | |
let x: { foo?: string }; | |
let y: { foo: string | undefined }; | |
x = {}; // ✅ The value can be omitted entirely, even `undefined` | |
y = {}; // ❌ The value must be explicitly provided, even if it is `undefined` | |
y = { foo: undefined }; // ✅ `undefined` is explicitly assigned |
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
// If you need to create a text dictionary with not all of it keys | |
type TermDictionary<TKey extends string> = Partial<Record<TKey, string>>; | |
type StatusUnion = 'Success' | 'Error' | 'Unknown'; | |
enum StatusEnum { | |
Success, | |
Error, | |
Unknown, |
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 Key<T extends object> = keyof T; | |
type Keys<T extends object> = Array<Key<T>>; | |
function keysOf<T extends object>(value: T): Keys<T> { | |
const keys: string[] = Object.keys(value); | |
return keys as Keys<T>; | |
} |
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 Swap<T, USubject, UTarget> = T extends USubject ? Exclude<T, USubject> | UTarget : T; | |
type Whatever = string | null; | |
// Swap null to undefined | |
type Swapped = Swap<Whatever, null, undefined>; // string | undefined | |
// TODO: Make a nested object implementation |
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
// Extendable regular object | |
type Extends<T extends object> = T & Record<string, unknown>; | |
// Extendable object with extendable nested objects | |
type Extendable<T> = Extends<{ [Key in keyof T]: T[Key] extends object ? Extends<T[Key]> : T[Key] }>; | |
type Employee = { | |
name: string, | |
age: 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
// Unreliable object 😱 | |
type UnsafePerson = { | |
name?: string | null, | |
age?: number | null, | |
} | |
// Nested unreliable object 😱😱😱 | |
type UnsafeParent = UnsafePerson & { child: UnsafePerson }; | |
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 SizeToken = 'XSmall' | 'Small' | 'Medium' | 'Large' | 'XLarge'; | |
type ElementSizeVariant<T = string> = T extends SizeToken ? T | 'Medium' : { width: number, height: number }; | |
type ButtonSize = ElementSizeVariant<'Small' | 'Large'>; // 'Small' | 'Large' | 'Medium'; | |
type CardSize = ElementSizeVariant; // { width: number, height: 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
// with infer keyword | |
type ArrayItemInfer<T> = T extends Array<infer I> ? I : never; | |
// or just with regular index type 🙃 | |
type ArrayItemRegular<T extends Array<any>> = T[number]; | |
type People = { firstName: string; lastName: string; }[]; | |
type PersonInfer = ArrayItemInfer<People>; // { firstName: string; lastName: 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
type Validator<T> = (value: T) => boolean; | |
class FormInput<T> { | |
constructor(public value: T, public validators: Validator<T>[]){} | |
get isValid(): boolean { | |
return this.validators.every(v => v(this.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
// Example entity | |
export interface IPerson { | |
firstName: string, | |
lastName: string, | |
age: number, | |
} | |
type SortOrder = 'asc' | 'desc'; // ascending or descending | |
type OrderByKeys<T> = `${keyof T & string}:${SortOrder}`[] |