能否定义含首字母大写字段名的BuckleScript记录?
Yes, you absolutely can define BuckleScript records with uppercase field names, and using [@bs.deriving jsConverter] will let you seamlessly convert between these records and JavaScript objects that have uppercase keys—exactly what you need for your use case.
Your example code is perfectly valid:
[@bs.deriving jsConverter] type coordinates = { X: int, Y: int };
This will auto-generate two handy conversion functions for you:
coordinatesToJs: Converts your BuckleScript record into a JavaScript object with matching uppercaseXandYproperties.coordinatesFromJs: Parses a JavaScript object with those uppercase keys back into your record type.
Here's how you'd use them in practice:
// Create a BuckleScript record let myCoordinates = {X: 42, Y: 100}; // Convert to a JavaScript object let jsObj = coordinatesToJs(myCoordinates); // jsObj now exists as { X: 42, Y: 100 } in JavaScript // Convert the JS object back to a BuckleScript record let parsedRecord = coordinatesFromJs(jsObj); // parsedRecord is the record {X: 42, Y: 100}
The official BuckleScript documentation includes a dedicated section on converting between JS objects and records that covers this scenario in detail. It confirms that the jsConverter deriver respects the exact case of your record field names when generating conversion logic, so you won't run into case mismatches with your target JavaScript objects.
内容的提问来源于stack exchange,提问作者GTDev

