Skip to content

Instantly share code, notes, and snippets.

View develax's full-sized avatar
💭
∑☯

Joe develax

💭
∑☯
View GitHub Profile
@develax
develax / TypeScript_Unions.md
Last active September 21, 2024 19:26
Unions as type property keys

Unions

Unions as type property keys

type StringKeys = 'firstName' | 'lastName';
type NumberKeys = 'age';

type Person = {
    [Key in StringKeys]: string;

Infer Function Argument Type

A function that accepts an argument of an anonymous type where hobbies is a tuple:

function stringifyPerson(person: {
    name: string;
    age: number;
    hobbies: [string, string]; // tuple
}) {
    return `${person.name} is ${person.age} years old and loves ${person.hobbies.join(" and  ")}.`;
@develax
develax / TypeScript_Infer.md
Last active September 23, 2024 19:41
TypeScript inferring types

TypeScript infer

Extract String

type ExtractStringType<T> = T extends `${infer U}` ? U : never;
type StrType1 = ExtractStringType<'ABC'>; // 'ABC'
type StrType2 = ExtractStringType<string>; // never
type StrType3 = ExtractStringType<123>; // never
@develax
develax / VSCode_Settings.MD
Last active November 12, 2024 08:22
VSCode Settings

VSCode Settings