在 Bun 中執行程式碼時進行條件檢測的推薦方法是檢查全域性變數 Bun 是否存在。
這類似於在瀏覽器中檢測 window 變數是否存在來判斷程式碼是否在瀏覽器中執行。
if (typeof Bun !== "undefined") {
// this code will only run when the file is run with Bun
}
在 TypeScript 環境中,如果不安裝 @types/bun,上述方法將導致型別錯誤。為避免這種情況,您可以改用檢查 process.versions。
if (process.versions.bun) {
// this code will only run when the file is run with Bun
}