Skip to content

Commit 246e26d

Browse files
committed
type tweaks
1 parent e9140c2 commit 246e26d

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

src/async.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
import { coinTypeMap } from "./consts/coinTypeMap.js";
22
import type {
3+
Coin,
34
CoinName,
45
CoinType,
56
CoinTypeInvertedReference,
67
Formats,
78
} from "./types.js";
89

9-
export const getCoderByCoinNameAsync = async <TCoinName extends CoinName>(
10+
export const getCoderByCoinNameAsync = async <
11+
TCoinName extends CoinName | string = string
12+
>(
1013
name: TCoinName
11-
): Promise<Formats[TCoinName]> => {
14+
): Promise<TCoinName extends CoinName ? Formats[TCoinName] : Coin> => {
1215
const mod = await import(`./coin/${name}`);
1316
if (!mod) {
1417
throw new Error(`Unsupported coin: ${name}`);
1518
}
1619
return mod[name];
1720
};
1821

19-
export const getCoderByCoinTypeAsync = async <TCoinType extends CoinType>(
22+
export const getCoderByCoinTypeAsync = async <
23+
TCoinType extends CoinType | number = number
24+
>(
2025
coinType: TCoinType
21-
): Promise<CoinTypeInvertedReference[TCoinType]> => {
26+
): Promise<
27+
TCoinType extends CoinType ? CoinTypeInvertedReference[TCoinType] : Coin
28+
> => {
2229
const name = coinTypeMap[String(coinType) as keyof typeof coinTypeMap];
2330
if (!name) {
2431
throw new Error(`Unsupported coin type: ${coinType}`);

src/index.ts

+27-8
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,51 @@
11
import * as formats from "./coins.js";
22
import { coinTypeMap } from "./consts/coinTypeMap.js";
33
import type {
4+
Coin,
45
CoinName,
56
CoinType,
67
CoinTypeInvertedReference,
8+
DecoderFunction,
9+
EncoderFunction,
710
Formats,
811
} from "./types.js";
912

10-
export const getCoderByCoinName = <TCoinName extends CoinName>(
13+
export type {
14+
Coin,
15+
CoinName,
16+
CoinType,
17+
CoinTypeInvertedReference,
18+
DecoderFunction,
19+
EncoderFunction,
20+
Formats,
21+
};
22+
23+
export const getCoderByCoinName = <
24+
TCoinName extends CoinName | string = string
25+
>(
1126
name: TCoinName
12-
): Formats[TCoinName] => {
13-
const format = formats[name];
27+
): TCoinName extends CoinName ? Formats[TCoinName] : Coin => {
28+
const format = formats[name as keyof typeof formats];
1429
if (!format) {
1530
throw new Error(`Unsupported coin: ${name}`);
1631
}
17-
return format;
32+
return format as TCoinName extends CoinName ? Formats[TCoinName] : Coin;
1833
};
1934

20-
export const getCoderByCoinType = <TCoinType extends CoinType>(
35+
export const getCoderByCoinType = <
36+
TCoinType extends CoinType | number = number
37+
>(
2138
coinType: TCoinType
22-
): CoinTypeInvertedReference[TCoinType] => {
39+
): TCoinType extends CoinType ? CoinTypeInvertedReference[TCoinType] : Coin => {
2340
const name = coinTypeMap[String(coinType) as keyof typeof coinTypeMap];
2441
if (!name) {
2542
throw new Error(`Unsupported coin type: ${coinType}`);
2643
}
27-
const format = formats[name] as CoinTypeInvertedReference[TCoinType];
44+
const format = formats[name];
2845
if (!format) {
2946
throw new Error(`Unsupported coin type: ${coinType}`);
3047
}
31-
return format;
48+
return format as TCoinType extends CoinType
49+
? CoinTypeInvertedReference[TCoinType]
50+
: Coin;
3251
};

0 commit comments

Comments
 (0)