Skip to content

Instantly share code, notes, and snippets.

@royletron
Created July 31, 2025 15:01
Show Gist options
  • Select an option

  • Save royletron/87e8b5b86e6e37aa1762807c1a540b31 to your computer and use it in GitHub Desktop.

Select an option

Save royletron/87e8b5b86e6e37aa1762807c1a540b31 to your computer and use it in GitHub Desktop.
Typescript nobbery
type KeyType = "boo" | "poo";
function filterByType<T extends { key: KeyType }, K extends KeyType>(
children: T[],
type: K
): (T & { key: K })[] {
return children.filter((child) => child.key === type) as (T & { key: K })[];
}
const boos = filterByType(
[
{ key: "boo", value: "boo" },
{ key: "poo", value: "poo" },
],
"boo"
);
boos[0].key;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment