JaylyDev / scriptapi-docs

Script API Documentation Generator (Stable and Preview)
https://jaylydev.github.io/scriptapi-docs/
MIT License
16 stars 3 forks source link

Consider adding available `console` methods to the common typing definitions #10

Open ChristopherHaws opened 1 month ago

ChristopherHaws commented 1 month ago

The docs make use of console.log, console.warn, etc for logging messages to the Content Log, however, in order to get access to those API's, you have to add the DOM lib to the tsconfig.json file which is not actually correct since almost all of the types from DOM are not available in the MC Bedrock runtime.

To solve this locally, I have a global.d.ts file where I define the 4 console methods which MC Bedrock does support. It would be really helpful if these types could get added to @minecraft/common so they are available out of the box.

Here are the typings which I am using:

declare global {
    interface Console {
        error(...data: any[]): void;
        info(...data: any[]): void;
        log(...data: any[]): void;
        warn(...data: any[]): void;
    }

    var console: Console;
}

// An export is needed to ensure that TypeScript treats the file as a module.
// Without this, TypeScript could potentially treat this file as a script,
// which could lead to global declarations not being recognized correctly.
export {};
JaylyDev commented 2 weeks ago

I'll take a look on this. These global variables are binded to the engine rather than the @minecraft/common native module so having a global.d.ts is definitely preferred.

ChristopherHaws commented 2 weeks ago

@JaylyDev Are there any other global variables which are bound to the engine which I am missing in my global.d.ts file? As far as I can tell, they are not documented anywhere.

One possible solution to this problem would be to create a custom npm package containing the global bindings and overriding one of the built-in lib definitions. Support for this was added in TS 4.5 (see this blog post for more info: https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#supporting-lib-from-node_modules).

With a custom lib package, a user who wants MC types instead of DOM types could simply:

{
 "dependencies": {
    // Override the built-in DOM lib
    "@typescript/lib-dom": "npm:@minecraft/lib"
  }
}

As far as I know, there is not a way to add a custom lib type, only override existing ones. I picked DOM here since it is where the standard console.* methods live, but scripthost might be a more fitting alternative.

ChristopherHaws commented 2 weeks ago

Another solution could be to provide a custom tsconfig base (similar to https://github.com/tsconfig/bases).

The custom base tsconfig could provide a type import for the global runtime types while also making it simpler for people to configure their TS environments.

For example, the current tsconfig might currently look similar to this (pulled from https://github.com/microsoft/minecraft-scripting-samples/blob/main/ts-starter/tsconfig.json):

{ 
    "compilerOptions":{ 
       "target":"es6",
       "moduleResolution":"Node",
       "module": "ES2020",
       "declaration":false,
       "noLib":false,
       "emitDecoratorMetadata":true,
       "experimentalDecorators":true,
       "sourceMap":true,
       "pretty":true,
       "forceConsistentCasingInFileNames": true,
       "strict": true,
       "allowUnreachableCode":true,
       "allowUnusedLabels":true,
       "noImplicitAny":true,
       "noImplicitReturns":false,
       "noImplicitUseStrict":false,
       "outDir":"lib",
       "rootDir": ".",
       "baseUrl":"behavior_packs/",
       "listFiles":false,
       "noEmitHelpers":true,
       "skipLibCheck": true
    },
    "include":[ 
       "scripts/**/*"
    ],
    "exclude": ["lib", "dist", "node_modules"],
    "compileOnSave":false
 }

This requires the developer to know the ECMA Script version that MC Bedrock's JS engine supports which I couldn't find any docs on. Centralizing the tsconfig would allow you more control over these settings, including adding any custom type's, and the addon's tsconfig could be shortened to:

{
    "extends": "@minecraft/common/tsconfig.json",
    "include": ["scripts/**/*"],
    "compilerOptions": {
       "rootDir": ".",
       "outDir":"lib",
       "baseUrl":"behavior_packs/",
    }
}