Files
scriptc/tests/corpus/3021-buffer-union-narrow-callback.ts
Chris Tate b5c0d78d0d Fix Buffer.from on narrowed callback unions (#422)
- Extract the checker-proven source arm before constructing a Buffer from a callback value.
- Cover narrowed byte and number array callbacks with Node differential fixtures.
2026-09-25 07:14:19 -05:00

25 lines
792 B
TypeScript

type SqlParam = string | number | boolean | null | Uint8Array;
function encodeParam(param: SqlParam): string | number | boolean | null | { $hex: string } {
if (param !== null && typeof param === "object") {
return { $hex: Buffer.from(param).toString("hex") };
}
return param;
}
function encodeParams(params: SqlParam[]): string {
return JSON.stringify(params.map(encodeParam));
}
console.log(encodeParams(["a", 1, true, null, new Uint8Array([0xde, 0xad])]));
type ListParam = string | number[] | null;
function encodeList(param: ListParam): string {
if (Array.isArray(param)) return Buffer.from(param).toString("hex");
return param === null ? "null" : param;
}
const listParams: ListParam[] = [null, [1, 254], "text"];
console.log(listParams.map(encodeList).join("|"));