Yes, it's possible to create functions in TypeScript that compute time durations and have their return types reflect the computed values, especially for literal inputs. However, there are limitations due to the nature of TypeScript's type system.
TypeScript can represent literal types and perform some type-level arithmetic using recursive conditional types and tuple manipulation. But when it comes to operations like multiplication with large numbers (e.g., multiplying by 1000), the type system has its limitations because it isn't designed to perform arbitrary arithmetic computations at the type level.
For small numbers, we can create utility types to perform arithmetic operations and achieve the desired effect. Below, I'll demonstrate how you might implement some of these functions with auto-generated return types for small integer inputs.
// Type utility to create a tuple with N elements
type BuildTuple<L extends number, T extends any[] = []> = T['length'] extends L ? T : BuildTuple<L, [...T, any]>;
// Addition
type Add<A extends number, B extends number> = [...BuildTuple<A>, ...BuildTuple<B>]['length'];
// Subtraction
type Subtract<A extends number, B extends number> = BuildTuple<A> extends [...BuildTuple<B>, ...infer R] ? R['length'] : never;
// Multiplication (recursive)
type Multiply<A extends number, B extends number, Result extends any[] = []> = {
finish: Result['length'];
next: Multiply<A, Subtract<B, 1>, [...Result, ...BuildTuple<A>]>;
}[B extends 0 ? 'finish' : 'next'];
// Specific type for milliseconds
type Milliseconds<N extends number> = Multiply<N, 1000>;
// Functions
function seconds<N extends number>(n: N): Milliseconds<N> {
return (n * 1000) as Milliseconds<N>;
}
// Usage
const oneSecond = seconds(1); // Type is 1000
const twoSeconds = seconds(2); // Type is 2000
This approach works for small numbers (typically numbers less than 50 due to TypeScript's recursion limits). However, when you try to multiply larger numbers, especially by 1000, you'll quickly run into these limitations.
Limitations:
- Recursion Depth: TypeScript enforces a maximum recursion depth for type aliases (usually around 50). Multiplying large numbers would exceed this limit.
- Performance: Type-level computations can significantly slow down your type-checking, leading to decreased developer experience.
- Complexity: The types become increasingly complex and harder to maintain, especially when you try to support a wide range of inputs.
Alternative Approach:
Given these limitations, a more practical approach is to:
- Use TypeScript's constant literal types for specific known values.
- Document the expected return values using JSDoc comments.
- Let the function return type be
number while providing runtime correctness.
Here's how you might implement it:
/**
* Converts seconds to milliseconds.
* @param n - The number of seconds.
* @returns The number of milliseconds.
*/
function seconds(n: number): number {
return n * 1000;
}
/**
* Converts minutes to milliseconds.
* @param n - The number of minutes.
* @returns The number of milliseconds.
*/
function minutes(n: number): number {
return n * 60 * 1000;
}
// And so on for other units...
// Usage
const oneSecond = seconds(1); // Type is number, value is 1000
const twoMinutes = minutes(2); // Type is number, value is 120000
While this doesn't auto-generate the return type to be the exact computed number at the type level, it provides the correct computation at runtime and keeps the type definitions simple and maintainable.
Conclusion:
Creating functions that auto-generate precise return types based on arithmetic computations is theoretically possible for very small numbers in TypeScript. However, due to practical limitations, it's not feasible for general use, especially when dealing with larger numbers or a broad range of inputs.
Recommendation:
- Keep the return type as
number to ensure broad usability.
- Use JSDoc comments to document the function's behavior and expected outputs.
- Rely on runtime computations for calculating the exact values.
Example with JSDoc:
/**
* Converts seconds to milliseconds.
* @param n - The number of seconds.
* @returns The number of milliseconds (n * 1000).
* @example
* seconds(1); // 1000
* seconds(5); // 5000
*/
function seconds(n: number): number {
return n * 1000;
}
This way, developers using your functions can see from the documentation what the expected outputs are, without running into the complexities and limitations of the type system.