Dafny to JavaScript type conversion reference guide
Every type boundary between Dafny and JavaScript: numbers, strings, sequences, sets, maps, datatypes, tuples, and results — with copy-paste code examples.
Use this file to discover all available pages before exploring further.
Dafny compiles to JavaScript, but the two type systems do not map one-to-one. Dafny integers become BigNumber objects, strings become character sequences, and algebraic datatypes use constructor and destructor naming conventions. This page documents every type boundary you will encounter when writing app-extras.js wrappers or calling generated code directly.
Before calling any Dafny-generated functions, you must load the compiled .cjs bundle and configure BigNumber.
Client (Vite)
Server (Node.js)
import BigNumber from 'bignumber.js';BigNumber.config({ MODULO_MODE: BigNumber.EUCLID });import myAppCode from './MyApp.cjs?raw';const require = (mod) => { if (mod === 'bignumber.js') return BigNumber; throw new Error(`Unknown module: ${mod}`);};const initDafny = new Function('require', ` ${myAppCode} return { _dafny, MyAppDomain, AppCore };`);const { _dafny, AppCore } = initDafny(require);
import { readFileSync } from 'fs';import { fileURLToPath } from 'url';import { dirname, join } from 'path';import BigNumber from 'bignumber.js';BigNumber.config({ MODULO_MODE: BigNumber.EUCLID });const __filename = fileURLToPath(import.meta.url);const __dirname = dirname(__filename);const myAppCode = readFileSync(join(__dirname, 'MyApp.cjs'), 'utf-8');const require = (mod) => { if (mod === 'bignumber.js') return BigNumber; throw new Error(`Unknown module: ${mod}`);};const initDafny = new Function('require', ` ${myAppCode} return { _dafny, MyAppDomain, AppCore };`);const { _dafny, AppCore } = initDafny(require);
BigNumber.config({ MODULO_MODE: BigNumber.EUCLID }) must be called before any Dafny code runs. Dafny’s modulo semantics for negative numbers differ from JavaScript’s default, and skipping this line causes incorrect arithmetic.
Dafny integers compile to BigNumber objects from the bignumber.js library.
Direction
Pattern
JS → Dafny
new BigNumber(42)
Dafny → JS
bn.toNumber()
Zero constant
_dafny.ZERO
// JS → Dafnyconst dafnyInt = new BigNumber(42);// Dafny → JSconst jsNumber = dafnyInt.toNumber();// The zero constant from the _dafny runtimeconst zero = _dafny.ZERO;
When you need to customize the generated API — for example, to accept plain JavaScript types instead of Dafny types — create app-extras.js that wraps app.js:
Always call GeneratedApp functions rather than AppCore.__default directly inside app-extras.js. The generated app.js layer handles JS → Dafny type conversions. Calling AppCore.__default directly with raw JavaScript strings will fail because Dafny expects seq<char>, not a JS string.
The generated app.js includes toJson/fromJson for all datatypes:
// Serialize for network transportconst json = App.actionToJson(action);const action = App.actionFromJson(json);// Full model serializationconst modelJson = App.modelToJson(model);const model = App.modelFromJson(modelJson);
dafny2js tool
Generate JavaScript API wrappers automatically from your Dafny code.
app-extras pattern
Full guide to customizing the generated API without modifying generated code.