Last active
November 28, 2025 18:30
-
-
Save JakeSidSmith/c70ddb43f6e8578fae4821a6b22ebf1a to your computer and use it in GitHub Desktop.
Type assertions for items in an array
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
| const list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] as const; | |
| type List = typeof list; | |
| type CheckListItems<Items extends readonly number[]> = Items extends readonly [ | |
| infer First, | |
| ...infer Rest, | |
| ] | |
| ? First extends 3 | |
| ? 'fail' | |
| : Rest extends readonly number[] | |
| ? CheckListItems<Rest> | |
| : never | |
| : 'pass'; | |
| type Result = CheckListItems<List>; | |
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| const assertPass = <T extends 'pass'>() => null; | |
| assertPass<CheckListItems<List>>(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment