Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* eslint-disable @typescript-eslint/ban-types */

export type SplitTypes<T, U> = U extends T ? U : Exclude<T, U>;
export type SplitTypes<T, U> = U extends T
? Exclude<T, U> extends never ? T : Exclude<T, U>
: T;

export type SplitUndefined<T> = SplitTypes<T, undefined>;

export type ContextOf<ContextType> = ContextType extends undefined
Expand Down
4 changes: 2 additions & 2 deletions src/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ export const defaultDecodeOptions: DecodeOptions = {};
*
* This is a synchronous decoding function. See other variants for asynchronous decoding: `decodeAsync()`, `decodeStream()`, `decodeArrayStream()`.
*/
export function decode<ContextType>(
export function decode<ContextType = undefined>(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buffer: ArrayLike<number> | ArrayBuffer,
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
): unknown {
const decoder = new Decoder<ContextType>(
const decoder = new Decoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.maxStrLength,
Expand Down
6 changes: 3 additions & 3 deletions src/decodeAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function decodeAsync<ContextType>(
): Promise<unknown> {
const stream = ensureAsyncIterabe(streamLike);

const decoder = new Decoder<ContextType>(
const decoder = new Decoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.maxStrLength,
Expand All @@ -27,7 +27,7 @@ export function decodeArrayStream<ContextType>(
) {
const stream = ensureAsyncIterabe(streamLike);

const decoder = new Decoder<ContextType>(
const decoder = new Decoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.maxStrLength,
Expand All @@ -46,7 +46,7 @@ export function decodeStream<ContextType>(
) {
const stream = ensureAsyncIterabe(streamLike);

const decoder = new Decoder<ContextType>(
const decoder = new Decoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.maxStrLength,
Expand Down
4 changes: 2 additions & 2 deletions src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ const defaultEncodeOptions: EncodeOptions = {};
*
* 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`.
*/
export function encode<ContextType>(
export function encode<ContextType = undefined>(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless giving undefined as default type, the following code will cause type mismatch errors:

encode(someValue, { sortKeys: true })

Even though this error can be avoidable by giving explicit type annotation to the code above like encode(someValue, { sortKeys: true } as EncodeOptions), this will be braking change of the API.

value: unknown,
options: EncodeOptions<SplitUndefined<ContextType>> = defaultEncodeOptions as any,
): Uint8Array {
const encoder = new Encoder<ContextType>(
const encoder = new Encoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.maxDepth,
Expand Down