Open wbt opened 1 year ago
Thanks for reporting this @wbt! Yeah it looks like our types are messed up in contract-schema. In the meantime, enabling the skipLibCheck
in your tsconfig.json
might be a valid workaround. Hopefully we can figure out a solution to this eventually. Currently we don't plan on removing those types.
Thanks for the note!
Unfortunately, skipLibCheck
seems to be a rather blunt instrument that suppresses all errors in .d.ts files. For several months I was using a tsconfig that extended a standard one where that option was set to true, and it made for a lot of very hard-to-debug errors showing up in .ts files ultimately traceable back to errors in project-specific .d.ts files that set up libraries of types for use in the project. We determined it was way better to enable typechecking on those files and any issues that crop up get flagged right away with high visibility in the IDE, so they get fixed at the source instead of propagating to places where the error messages do not provide any information that is helpful in diagnosing and fixing the issue.
Ultimately, I expect this will be best fixed in the json-schema-to-typescript library, but in the interim we are considering running a little script that goes into the node-modules folder and modifies the definition to add that one |undefined
on the index type. Having that run in the Truffle repository between the json-schema-to-typescript and the publication of a package on npm would fix it for more than just my colleagues and I - would Truffle folks appreciate re-contribution of this band-aid?
Hardcoding type definitions in Truffle (starting with the autogenerated version) is another option and could address the other issues like what's called out in #4825, but would reduce maintainability by requiring that any changes to the JSON spec also be manually reflected in the types.
Thanks for https://github.com/trufflesuite/truffle/pull/5747 @wbt! To be honest this part of the codebase is not my specialty. Do you have anything helpful to add about this @gnidan?
Checking in on this, as it's still a script we're running and may still be a blocking issue for others.
In addition to issues duplicating #4825 (closed & I can't reopen, but for a different package) about types being messed up, in @truffle/contract-schema the index.d.ts file triggers 21 errors on compilation, related to possibly-undefined values not being assignable to a definitely-defined index type:
I don't think that simply removing this file (as in #5142), especially on a patch-level bump, is the right solution, as having some type in there, even if it has errors, gives the compiler more to work with. Adding
| undefined
after line 137 takes care of the errors (not all the issues, but at least the TS compilation errors) in a way that wouldn't break any existing code (as far as I can tell). Changing that in the generated file would require changing the generation script. There is a comment just above that index type definition that This interface was referenced byContractObject
's JSON-Schema definition via thepatternProperty
"^x-". Maybe the change should happen in the JSON-Schema definition, maybe it's in the patternProperty, or maybe it's in the script that parses both, but this is at least a hint.