Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

add NumberToString conversion map #7

Merged
merged 2 commits into from
Jul 31, 2017
Merged

Conversation

amir-arad
Copy link

useage:

type MyType<I extends number> = ... NumberToString[I]

useage:
```ts
type MyType<I extends number> = ... NumberToString[I]
```
@gcanti
Copy link
Owner

gcanti commented Jul 31, 2017

@amir-arad how do you use this type? I'm trying to do the following but got some errors (ts v2.4.2)

import { StringEq } from '../src'

export type NumberToString = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

// Type 'N' cannot be used to index type '["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]'
export type GetString<N extends number> = NumberToString[N]

// Type 'A' cannot be used to index type '["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]'.
// Type 'B' cannot be used to index type '["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]'.
export type NumberEq<A extends number, B extends number> = StringEq<NumberToString[A], NumberToString[B]>

@amir-arad
Copy link
Author

amir-arad commented Jul 31, 2017

I use it percisely to avoid such errors in different scenarios :)

declare type THListLengthNum<L extends THList> = {
    true: 0;
    false: Increment[THListLengthNum<THListTail<L>>];
}[THListIsTHNil<L>];
type Func<A extends THList, ...> = {
0:...
1:...
2:...
...
}[NumberToString[THListLengthNum<A>]];

were I to use [THListLengthNum<A>] in the last line I'd have gotten an error.

@KiaraGrouwstra
Copy link

Wow, I love the workaround. That hadn't occurred to me so far.
I guess then its opposite StringToNumber may serve to resolve the same error for this one.
Anyway, I'd filed this issue earlier at microsoft/TypeScript#15768.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants