create3000 / x_ite

X_ITE X3D Browser, view and manipulate X3D, VRML, glTF and other 3D sources in HTML.
https://create3000.github.io/x_ite/
Other
67 stars 15 forks source link

Type definitions are broken, errors, problems in .d.ts #181

Closed krisutofu closed 1 month ago

krisutofu commented 1 month ago

Using Typescript 5.6.2, VSCode 1.93.1.

I recently discovered X3D and I tried to start writing code for generating a scene into HTML. After installing the package and setting up the typescript configuration, it cannot find the types like X3DBrowser, ProfileInfo or X3DScene. I can only default-import X3D but proper types are not accessible in type expressions.

import X3D from "x_ite"  // no swiggles here but does not provide useful types
//import { X3D, X3DBrowser } from "x_ite" // error

let browser: X3D.X3DBrowser = X3D.getBrowser()  // getBrowser() is found, X3DBrowser is not a type however

Same for equivalent imports.

My typescript configuration is

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig to read more about this file */

    /* Projects */
    "incremental": true,                              /* Save .tsbuildinfo files to allow for incremental compilation of projects. */

    /* Language and Environment */
    "target": "ES2023",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */

    /* Modules */
    "module": "ES2022",                                /* Specify what module code is generated. */
    "rootDir": "./src/",                                  /* Specify the root folder within your source files. */
    "moduleResolution": "bundler",                     /* Specify how TypeScript looks up a file from a given module specifier. */

    /* Emit */
    "outDir": "./dist/",                                   /* Specify an output folder for all emitted files. */

    /* Interop Constraints */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */

    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "strictNullChecks": true,                         /* When type checking, take into account 'null' and 'undefined'. */
    "strictFunctionTypes": true,                      /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
    "strictBindCallApply": true,                      /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
    "noImplicitThis": true,                           /* Enable error reporting when 'this' is given the type 'any'. */
  }
}

(The additional strict settings are probably redundant.)

I found, properties cannot be used as types in Typescript. var browser: X3D.X3DBrowser will fail but var browser: X3D["X3DBrowser"] works which is short for typeof X3D.X3DBrowser (in the case of X3D being an interface).

I found many errors (red swiggles) in the .d.ts file, including typescript errors (export = … cannot be used together with export default, wrong type number?), references to non-existing types fieldProxy and X3DMaterialExtensionNodeProxy in the autogenerated code, typing errors (overriding type rule checks failed).

I fixed all of the problems and eventually made it work as originally intended by the code (using a namespace instead of an interface). I gonna make a PR draft only for the purpose of showing the changes that work for me. Unfortunately, the autogenerated code is affected too (undefined type names and wrong override types).

create3000 commented 1 month ago

New patched version 10.5.5 is out now, so I will close this issue.