Skip to content

Commit

Permalink
NBT Primitive Type Validation
Browse files Browse the repository at this point in the history
Been looking into TypeScript's other tsconfig options, and I realized it would probably be of good use for me to try enabling more of the strict features that aren't already enabled with the 'strict' flag.

Was first looking into enabling 'noUncheckedIndexAccess', to which I then discovered one I hadn't heard of yet, 'noPropertyAccessFromIndexSignature'! This heavily helps with the NBT primitive type validation for the library's API parameter types, making use of the original index signature-based checks. This helps out so much, because it prevents you from accessing non-existant properties on the index signature-implemented structures, unless you use index-based accessing.

https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess
https://www.typescriptlang.org/tsconfig#noPropertyAccessFromIndexSignature
https://www.npmjs.com/package/@tsconfig/strictest

#28
  • Loading branch information
Offroaders123 committed Jul 17, 2023
1 parent 6eaa353 commit 9fbc2aa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
19 changes: 19 additions & 0 deletions test/builder-class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { write, Int32 } from "../src/index.js";

import type { Tag, IntTag, CompoundTag } from "../src/tag.js";

export type Difficulty = IntTag<0 | 1 | 2 | 3>;

export interface BedrockLevelDatLike extends CompoundTag {
Difficulty: Difficulty;
}

export class BedrockLevelDat implements BedrockLevelDatLike {
[name: string]: Tag;

Difficulty: Difficulty = new Int32(2);
}

const levelDat = new BedrockLevelDat();

await write(levelDat);
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"target": "ESNext",
"noEmit": true,
"strict": true,
"noImplicitOverride": true
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true
}
}

0 comments on commit 9fbc2aa

Please sign in to comment.