Skip to content

Commit

Permalink
Reverting NBT Primitive Type Narrowing
Browse files Browse the repository at this point in the history
Removing the API-safe `CompoundTag` type checking for the library entry points, in the meantime. Without the index signature key value checking behavior that I need to accomplish the value type checking, there isn't another way to create API designs that will make the compiler happy, unfortunately. So this means I'm going to make all objects acceptable into NBTify, on the type level at least. Since I can't use type checking to validate the types for the shapes I'm passing in, I will just have to allow them in, and make sure that the shapes have appropriate value types.

While working on this revert, I looked into the `object` type, and realized it's kind of similar in a way as to what I'm trying to do with my primitive interface type checkers (`ListTag` and `CompoundTag`).

> Introduction to TypeScript `object` type
> The TypeScript `object` type represents all values that are not in primitive types.
>
> The following are primitive types in TypeScript:
>
> `number`
> `bigint`
> `string`
> `boolean`
> `null`
> `undefined`
> `symbol`

For my object definitions, I'm trying to define an object with keys that are only of a select few types. For the `object` type, it represents all JavaScript types that aren't a primitive type. So with this change, NBTify simply only checks if your `CompoundTag` is none of the primitive values mentioned above, which is better than just using `any` at least. It would be better if the check could also specify that your `CompoundTag` object cannot have things like `Function`s, `symbol`s, `undefined`, `null`, things like that. I think that's my main reason for wanting to add parameter type checking for your `CompoundTag` use.

https://www.typescripttutorial.net/typescript-tutorial/typescript-object-type/

microsoft/TypeScript#52222
(#28)
  • Loading branch information
Offroaders123 committed May 11, 2023
1 parent 592bffa commit 570ad01
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
6 changes: 2 additions & 4 deletions src/data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Int } from "./primitive.js";

import type { CompoundTag } from "./tag.js";

export type Name = string | null;
export type Endian = "big" | "little";
export type Compression = "gzip" | "deflate";
Expand All @@ -17,14 +15,14 @@ export interface NBTDataOptions {
/**
* An object which represents a set of NBT data.
*/
export class NBTData<T extends CompoundTag = any> {
export class NBTData<T extends object = any> {
declare readonly data: T;
declare readonly name: Name;
declare readonly endian: Endian;
declare readonly compression: Compression | null;
declare readonly bedrockLevel: BedrockLevel | null;

constructor(data: CompoundTag | NBTData<T>, { name, endian, compression, bedrockLevel }: NBTDataOptions = {}) {
constructor(data: T | NBTData<T>, { name, endian, compression, bedrockLevel }: NBTDataOptions = {}) {
if (data instanceof NBTData){
if (name === undefined) name = data.name;
if (endian === undefined) endian = data.endian;
Expand Down
4 changes: 2 additions & 2 deletions src/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface ReadOptions {
*
* If a format option isn't specified, the function will attempt reading the data using all options until it either throws or returns successfully.
*/
export async function read<T extends CompoundTag = any>(data: Uint8Array | ArrayBufferLike, { endian, compression, strict, isNamed, isBedrockLevel }: ReadOptions = {}){
export async function read<T extends object = any>(data: Uint8Array | ArrayBufferLike, { endian, compression, strict, isNamed, isBedrockLevel }: ReadOptions = {}){
if (data instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && data instanceof SharedArrayBuffer){
data = new Uint8Array(data);
}
Expand Down Expand Up @@ -144,7 +144,7 @@ export class NBTReader {
/**
* Initiates the reader over an NBT buffer.
*/
read<T extends CompoundTag = any>(data: Uint8Array | ArrayBufferLike, { endian = "big", strict = true, isNamed = true }: NBTReaderOptions = {}) {
read<T extends object = any>(data: Uint8Array | ArrayBufferLike, { endian = "big", strict = true, isNamed = true }: NBTReaderOptions = {}) {
if (data instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && data instanceof SharedArrayBuffer){
data = new Uint8Array(data);
}
Expand Down
14 changes: 7 additions & 7 deletions test/LCEPlayer.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ByteTag, ShortTag, IntTag, FloatTag, DoubleTag, StringTag, ListTag, CompoundTag } from "../src/index.js";
import { ByteTag, ShortTag, IntTag, FloatTag, DoubleTag, StringTag } from "../src/index.js";

export interface LCEPlayer extends CompoundTag {
export interface LCEPlayer {
GamePrivileges: IntTag;
EnderItems: ListTag<LCEInventoryItem>;
EnderItems: LCEInventoryItem[];
abilities: {
walkSpeed: FloatTag;
flySpeed: FloatTag;
Expand All @@ -25,7 +25,7 @@ export interface LCEPlayer extends CompoundTag {
SleepTimer: ShortTag;
Sleeping: ByteTag;
SelectedItemSlot: IntTag;
Inventory: ListTag<LCEInventoryItem>;
Inventory: LCEInventoryItem[];
HurtByTimestamp: ShortTag;
FallFlying: ByteTag;
XpTotal: IntTag;
Expand All @@ -37,7 +37,7 @@ export interface LCEPlayer extends CompoundTag {
FallDistance: FloatTag;
DeathTime: ShortTag;
TimeSinceRest: IntTag;
Attributes: ListTag<LCEPlayerAttribute>;
Attributes: LCEPlayerAttribute[];
XpLevel: IntTag;
Health: FloatTag;
DataVersion: IntTag;
Expand All @@ -56,14 +56,14 @@ export interface LCEPlayer extends CompoundTag {
XpSeed: IntTag;
}

export interface LCEInventoryItem extends CompoundTag {
export interface LCEInventoryItem {
Damage: ShortTag;
Count: ByteTag;
id: StringTag;
Slot: ByteTag;
}

export interface LCEPlayerAttribute extends CompoundTag {
export interface LCEPlayerAttribute {
Base: DoubleTag;
ID: IntTag;
}
8 changes: 6 additions & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ console.log(recompile,"\n");

console.log(Buffer.compare(data,recompile));

interface TempTest extends NBT.CompoundTag {
interface TempTest {
noice: NBT.BooleanTag;
}

Expand All @@ -27,4 +27,8 @@ const { data: tempTest } = new NBT.NBTData<TempTest>({

tempTest.noice
// @ts-expect-error
tempTest.notAProperty
tempTest.notAProperty

const demo = new NBT.NBTData({ nice: true, smartTypes: 10 });

demo.data.smartTypes;
2 changes: 2 additions & 0 deletions test/nbt/invalid.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-nocheck

import { Byte, Short, NBTData } from "../../src/index.js";

export default new NBTData({
Expand Down

0 comments on commit 570ad01

Please sign in to comment.