Summary
Buffer.from() on a union narrowed to its object arm crashes when the enclosing function is used as a value (e.g. as an Array.prototype.map callback):
error SC9001: internal compiler error: in encodeParam: bytesNew source of kind union — please report this
Reproduction
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])]))
Actual
repro.ts:4:23 - error SC9001: internal compiler error: in encodeParam: bytesNew source of kind union — please report this
Expected
Compiles - param is narrowed to Uint8Array by the typeof param === 'object' guard.
Notes
- Calling
encodeParam(...) directly (not through .map) compiles; the crash appears when the function is used as a value/callback.
- The same call flow compiled under 0.0.36.
Workaround
Bind the narrowed value to an explicitly typed local first:
if (param !== null && typeof param === 'object') {
const bytes: Uint8Array = param
return {$hex: Buffer.from(bytes).toString('hex')}
}
Environment
- scriptc 0.1.4
- Node v24.14.0
- macOS (Darwin arm64)
Summary
Buffer.from()on a union narrowed to its object arm crashes when the enclosing function is used as a value (e.g. as anArray.prototype.mapcallback):Reproduction
Actual
Expected
Compiles -
paramis narrowed toUint8Arrayby thetypeof param === 'object'guard.Notes
encodeParam(...)directly (not through.map) compiles; the crash appears when the function is used as a value/callback.Workaround
Bind the narrowed value to an explicitly typed local first:
Environment