sjmulder / nbt-js

NBT.js – a JavaScript parser for NBT archives
http://sjmulder.github.io/nbt-js/
Other
73 stars 19 forks source link

TypeScript Types #45

Open Offroaders123 opened 1 year ago

Offroaders123 commented 1 year ago

Been working on a temporary fork of this library in TypeScript, just for the goal of adding .d.ts definitions for it's APIs. If merging them to the main project isn't a good option, I can publish them to DefinitelyTyped @types instead.

Contributing to DefinitelyTyped

NBT.js Type Definitions
sjmulder commented 1 year ago

Thank you for the work!

I'd be quite happy about such a definition file, in fact, maybe using TypeScript directly would be good too. But I haven't really maintained this project since it's early days when the JavaScript ecosystem was very different from now. I like that you can drop this in a browser outright without any compilation or bundling, but maybe that's behind the times.

Anyway re. this work, can you open a pull request for it?

Offroaders123 commented 1 year ago

I was going to push just the type definitions to start with, but I think it might as well be worth it to use TS in whole initially, rather than adding it later. Would you be up for that? I can make sure all of the tests and everything are still set up correctly too.

Offroaders123 commented 1 year ago

It will make the PR a bit easier to manage, and updates easier to keep track of too, since the type definitions would be driven fully by the TS file, because the source and typings would be both from the TS file, rather than having to update the two separately. Another alternative is that I could keep the types, but move the TS definition parts over to type-checked JSDoc types, that will also allow for .d.ts file generation. Sorry if that was a bit much to handle, I'll try some things out to see which may work best for this project specifically 😃

Offroaders123 commented 1 year ago

Ok, after trying some different things out, I'm going to go with the standalone .d.ts typings to start with. Moving to TypeScript as a whole can come as a separate step I think. That would involve adjusting some of the other tooling for the library, and I don't want to break anything unexpectedly in doing that. This will safely only affect things by providing types for the existing API.

All of these examples have proper typings in this image here! With the typings missing, each of the references to either nbt or parseUncompressed() would have red squiggles under them. *Edit: Decided to add an image for that.

*Edit: For context, the line with /// <reference types="nbt"/> is for situations when you load the library using a <script src="nbt.js"></script> context.

Standalone Library Typings Missing Library Typings Demo
Offroaders123 commented 1 year ago

I'd be quite happy about such a definition file, in fact, maybe using TypeScript directly would be good too. But I haven't really maintained this project since it's early days when the JavaScript ecosystem was very different from now. I like that you can drop this in a browser outright without any compilation or bundling, but maybe that's behind the times.

With big thanks to inspiration from your library and a few others out there, you may also be interested in my library NBTify! It follows this goal heavily in mind too, I also value being able to run things without needing a toolchain to use them.

Offroaders123 commented 12 months ago

I was wondering if you're still interested in merging the types for use with TypeScript? I submitted a pull request for it also.

peacefulotter commented 9 months ago

Any updates on this? I am not sure to use this package because it doesn't have typescript support yet!

Offroaders123 commented 9 months ago

I am not sure to use this package because it doesn't have typescript support yet!

In the meantime, if you'd like, I've been working on my own project like NBT.js which covers all the same bases that it does, also with ESM support and built-in compression handling. It's called NBTify. It has first-party TS support as well. NBTify

peacefulotter commented 9 months ago

Seems really nice, will move to yours! For those that still would like to use this package in Typescript, you can wrap this file into a declare module:

// nbt.d.ts
declare module 'nbt' {
  // ... file
}

And, optionally, add the file (or own types folder) to your typeRoots in tsconfig:

// tsconfig.json 
{
  "compilerOptions": {
    // ...
    "typeRoots": ["./node_modules/@types", "PATH_TO_nbt.d.ts"]
  }
}
Offroaders123 commented 9 months ago

Sorry to ask here, but I was wondering if you're on Discord as well? Been working with a few other people in implementing Minecraft-related things with web tech, and I'm curious in learning about your dedicated server project! Bun seems really cool too, haven't looked into that myself yet.

sjmulder commented 7 months ago

I've merged the pull request, but I think it's only fair to admit that I'm no good position to keep maintaining this repo. I'm glad there are active forks of people who are giving it the love and energy it deserves.

For me, I'm really not involved in Minecraft any longer and the JavaScript world has changed so much since initiating this project that I wouldn't be sure what direction to take it in. Take the vanilla JS approach, people don't really do that any more and 'compiled' JS has drifted significantly. Who should this project aim to serve? And I wouldn't be dogfooding.

Hopefully the forks benefit from having fresh, pragmatic maintainers with a vision :)