Created
April 19, 2023 01:48
-
-
Save webstrand/cb92d788062663ee37595135496d3211 to your computer and use it in GitHub Desktop.
Type-level integer to base-26 converter, excel column number style A = 0, AA = 26, AB = 27, etc
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 Tuple<L extends number, T extends any[] = []> = | |
| T["length"] extends L ? T : Tuple<L, [...T, void]>; | |
| type Keys = [ | |
| "Z", "Y", "X", "W", "V", "U", "T", "S", "R", | |
| "Q", "P", "O", "N", "M", "L", "K", "J", "I", | |
| "H", "G", "F", "E", "D", "C", "B", "A" | |
| ]; | |
| type Evil = { | |
| A: Tuple<1>, | |
| B: Tuple<2>, | |
| C: Tuple<3>, | |
| D: Tuple<4>, | |
| E: Tuple<5>, | |
| F: Tuple<6>, | |
| G: Tuple<7>, | |
| H: Tuple<8>, | |
| I: Tuple<9>, | |
| J: Tuple<10>, | |
| K: Tuple<11>, | |
| L: Tuple<12>, | |
| M: Tuple<13>, | |
| N: Tuple<14>, | |
| O: Tuple<15>, | |
| P: Tuple<16>, | |
| Q: Tuple<17>, | |
| R: Tuple<18>, | |
| S: Tuple<19>, | |
| T: Tuple<20>, | |
| U: Tuple<21>, | |
| V: Tuple<22>, | |
| W: Tuple<23>, | |
| X: Tuple<24>, | |
| Y: Tuple<25>, | |
| Z: Tuple<26>, | |
| } | |
| type ToExcel<T extends void[]> = ToExcel1<[...T], Keys, []>; | |
| type ToExcel1<T extends void[], L extends (keyof Evil)[], R extends void[]> = | |
| L extends [infer K extends keyof Evil, ...infer LC extends (keyof Evil)[]] | |
| ? T extends [...Evil[K], ...infer Over extends void[]] | |
| ? Over extends [] ? `${ToExcel1<R, Keys, []>}${K}`: ToExcel1<Over, Keys, [...R, void]> | |
| : ToExcel1<T, LC, R> | |
| : ""; | |
| type m = ToExcel<Tuple<560>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment