Would it be feasible to develop a utility type Number<T>
that can take a string literal type and convert it into a number? If conversion is not possible, should the utility return a never
type?
type Five = Number<'5'> // `Five` is considered as the number 5
In anticipation of addressing questions regarding the purpose of this utility:
The reason for posing this question is my attempt to create an Add
utility type for adding numbers.
type createArray<Len, Ele, Arr extends Ele[] = []> = Arr['length'] extends Len ? Arr : createArray<Len, Ele, [Ele, ...Arr]>
type Add<A extends number, B extends number> = [...createArray<A, 1>, ...createArray<B, 1>]['length']
At present, the utility functions correctly with:
type Answer = Add<3,10> // The answer is 13
However, it currently only works with the number
type. It would also be desirable for it to exclude the string
type, allowing functionality like this: type Answer = Add<'3','10'>