Skip to content

Commit d60d4e1

Browse files
authored
Merge pull request #139 from bananaumai/fix-type-checking-issue
fix: type check issues on encod/decode arguments
2 parents 1d5d519 + b623c69 commit d60d4e1

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

src/context.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/* eslint-disable @typescript-eslint/ban-types */
22

3-
export type SplitTypes<T, U> = U extends T ? U : Exclude<T, U>;
3+
export type SplitTypes<T, U> = U extends T
4+
? Exclude<T, U> extends never ? T : Exclude<T, U>
5+
: T;
6+
47
export type SplitUndefined<T> = SplitTypes<T, undefined>;
58

69
export type ContextOf<ContextType> = ContextType extends undefined

src/decode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ export const defaultDecodeOptions: DecodeOptions = {};
4242
*
4343
* This is a synchronous decoding function. See other variants for asynchronous decoding: `decodeAsync()`, `decodeStream()`, `decodeArrayStream()`.
4444
*/
45-
export function decode<ContextType>(
45+
export function decode<ContextType = undefined>(
4646
buffer: ArrayLike<number> | ArrayBuffer,
4747
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
4848
): unknown {
49-
const decoder = new Decoder<ContextType>(
49+
const decoder = new Decoder(
5050
options.extensionCodec,
5151
(options as typeof options & { context: any }).context,
5252
options.maxStrLength,

src/decodeAsync.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export async function decodeAsync<ContextType>(
99
): Promise<unknown> {
1010
const stream = ensureAsyncIterabe(streamLike);
1111

12-
const decoder = new Decoder<ContextType>(
12+
const decoder = new Decoder(
1313
options.extensionCodec,
1414
(options as typeof options & { context: any }).context,
1515
options.maxStrLength,
@@ -27,7 +27,7 @@ export function decodeArrayStream<ContextType>(
2727
) {
2828
const stream = ensureAsyncIterabe(streamLike);
2929

30-
const decoder = new Decoder<ContextType>(
30+
const decoder = new Decoder(
3131
options.extensionCodec,
3232
(options as typeof options & { context: any }).context,
3333
options.maxStrLength,
@@ -46,7 +46,7 @@ export function decodeStream<ContextType>(
4646
) {
4747
const stream = ensureAsyncIterabe(streamLike);
4848

49-
const decoder = new Decoder<ContextType>(
49+
const decoder = new Decoder(
5050
options.extensionCodec,
5151
(options as typeof options & { context: any }).context,
5252
options.maxStrLength,

src/encode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ const defaultEncodeOptions: EncodeOptions = {};
4343
*
4444
* The returned buffer is a slice of a larger `ArrayBuffer`, so you have to use its `#byteOffset` and `#byteLength` in order to convert it to another typed arrays including NodeJS `Buffer`.
4545
*/
46-
export function encode<ContextType>(
46+
export function encode<ContextType = undefined>(
4747
value: unknown,
4848
options: EncodeOptions<SplitUndefined<ContextType>> = defaultEncodeOptions as any,
4949
): Uint8Array {
50-
const encoder = new Encoder<ContextType>(
50+
const encoder = new Encoder(
5151
options.extensionCodec,
5252
(options as typeof options & { context: any }).context,
5353
options.maxDepth,

0 commit comments

Comments
 (0)