Offroaders123 / NBTify

A library to read and write NBT files on the web!
http://npm.im/nbtify
MIT License
42 stars 5 forks source link

List Tag Item Type Assertion #3

Closed Offroaders123 closed 1 year ago

Offroaders123 commented 2 years ago

Semi-related to #1, List tags need to have a way to define their element type, since JavaScript arrays can have items with multiple types, unlike how other languages do. I already made it so you can define the item type assertion on the TypeScript side, but this doesn't help with how the resulting JavaScript should write the List to an NBT Uint8Array. I'm thinking that possibly using Symbols could work, I'll have to try a few different things out.

Offroaders123 commented 1 year ago

Supported now! Rather than specifically declaring the value type on the array itself, the library will simply deduce what the type of the array is from the first item in the array. Since any undefined value in the NBT data object is invalid, this would simply be a TS type error and a runtime write error in the library itself. You can declare the item type when using the ListTag type in TS land, and that will simply be used for only type checking. I will add an error check that ensures that all array items must be of the same type when writing to a new raw NBT buffer (#14).