Created
July 31, 2025 15:01
-
-
Save royletron/87e8b5b86e6e37aa1762807c1a540b31 to your computer and use it in GitHub Desktop.
Typescript nobbery
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 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