trufflesuite / truffle

:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.
https://consensys.io/blog/consensys-announces-the-sunset-of-truffle-and-ganache-and-new-hardhat?utm_source=github&utm_medium=referral&utm_campaign=2023_Sep_truffle-sunset-2023_announcement_
MIT License
14.02k stars 2.32k forks source link

TS compilation errors in contract-schema #5746

Open wbt opened 1 year ago

wbt commented 1 year ago

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:

node_modules/@truffle/contract-schema/spec/index.d.ts:79:3 - error TS2411: Property 'contractName' of type 'string | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

79   contractName?: string;
     ~~~~~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:81:3 - error TS2411: Property 'metadata' of type 'string | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

81   metadata?: Metadata;
     ~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:85:3 - error TS2411: Property 'bytecode' of type 'string | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

85   bytecode?: string;
     ~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:89:3 - error TS2411: Property 'deployedBytecode' of type 'string | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

89   deployedBytecode?: string;
     ~~~~~~~~~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:93:3 - error TS2411: Property 'sourceMap' of type 'string | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

93   sourceMap?: string;
     ~~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:97:3 - error TS2411: Property 'deployedSourceMap' of type 'string | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

97   deployedSourceMap?: string;
     ~~~~~~~~~~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:98:3 - error TS2411: Property 'source' of type 'string | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

98   source?: Source;
     ~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:99:3 - error TS2411: Property 'sourcePath' of type 'string | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

99   sourcePath?: SourcePath;
     ~~~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:100:3 - error TS2411: Property 'ast' of type 'Ast | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

100   ast?: Ast;
      ~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:101:3 - error TS2411: Property 'legacyAST' of type 'LegacyAst | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

101   legacyAST?: LegacyAst;
      ~~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:102:3 - error TS2411: Property 'compiler' of type '{ [k: string]: any; name?: string | undefined; version?: string | undefined; } | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

102   compiler?: {
      ~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:107:3 - error TS2411: Property 'networks' of type '{ [k: string]: NetworkObject; } | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

107   networks?: {
      ~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:110:3 - error TS2411: Property 'schemaVersion' of type 'string | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

110   schemaVersion?: SchemaVersion;
      ~~~~~~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:111:3 - error TS2411: Property 'updatedAt' of type 'string | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

111   updatedAt?: string;
      ~~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:112:3 - error TS2411: Property 'networkType' of type 'string | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

112   networkType?: string;
      ~~~~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:113:3 - error TS2411: Property 'devdoc' of type 'NatSpec | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

113   devdoc?: NatSpec;
      ~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:114:3 - error TS2411: Property 'userdoc' of type 'NatSpec | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

114   userdoc?: NatSpec;
      ~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:115:3 - error TS2411: Property 'immutableReferences' of type 'ImmutableReferences | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

115   immutableReferences?: ImmutableReferences;
      ~~~~~~~~~~~~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:116:3 - error TS2411: Property 'generatedSources' of type 'GeneratedSources | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

116   generatedSources?: GeneratedSources;
      ~~~~~~~~~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:117:3 - error TS2411: Property 'deployedGeneratedSources' of type 'GeneratedSources | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

117   deployedGeneratedSources?: GeneratedSources;
      ~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@truffle/contract-schema/spec/index.d.ts:118:3 - error TS2411: Property 'db' of type '{ [k: string]: { [k: string]: any; id?: string | undefined; }; } | undefined' is not assignable to 'string' index type 'string | number | boolean | any[] | { [k: string]: any; }'.

118   db?: {
      ~~

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 by ContractObject's JSON-Schema definition via the patternProperty "^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.

eggplantzzz commented 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.

wbt commented 1 year ago

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.

eggplantzzz commented 1 year ago

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?

wbt commented 1 year ago

Checking in on this, as it's still a script we're running and may still be a blocking issue for others.