/** * Reverses a string. * - `T` The string to reverse. */ type Reverse< T extends string, _acc extends string = '', > = T extends `${infer Head}${infer Tail}` ? Reverse : _acc extends '' ? T : `${T}${_acc}` /** * A strongly-typed function to reverse a string. * @param str the string to reverse. * @returns the reversed string in both type level and runtime. * @example reverse('hello world') // 'dlrow olleh' */ function reverse(str: T) { return str.split('').reverse().join('') as Reverse } export type { Reverse } export { reverse }