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

Format Options Generic Merging #29

Closed Offroaders123 closed 1 year ago

Offroaders123 commented 1 year ago

Reference: d5f3b44

I haven't quite figured out how to merge the values of a generic from it's default type, and with that of those provided by the caller. Here's a better demo for that:

const demo = new NBTData({},{ /*name: "",*/ endian: "little", compression: "deflate-raw", bedrockLevel: null });

const { name, endian, compression, bedrockLevel } = demo;
name; // eek, shouldn't be `unknown`, should be of type `Name`.
endian; // "little"
compression; // "deflate-raw"
bedrockLevel; // null

This has to do with how I'm implementing the generic types for the NBTData object, specifically the U generic which is of the type FormatOptions. See how that is implemented as of this issue, here.

Offroaders123 commented 1 year ago

I think this can nearly be closed. The only issue that is currently present with this, is that the NBTData options generic parameter won't inherit the options of a parent NBTData object, if you were to use that as a format options object.

declare const data: Uint8Array;
const template = await NBT.read<any,{ endian: "big" }>(data);
const shadowed = new NBT.NBTData({},template);
shadowed.name; // Name
shadowed.endian; // "big"

Welp, after writing that out, and trying it over in VSCode, looks like it is working now! Maybe this can be closed sooner than I thought! Going to investigate some more that it is indeed working properly, before I actually formally close this one.