Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript build error on strict=true #52

Open
AndreMiras opened this issue Nov 19, 2023 · 3 comments
Open

TypeScript build error on strict=true #52

AndreMiras opened this issue Nov 19, 2023 · 3 comments

Comments

@AndreMiras
Copy link

I'm consuming chain-registry 1.20.0 with "strict": true in my tsconfig.json which leads to the following error:

node_modules/@chain-registry/types/types/chains.d.ts:177:9 - error TS2411: Property 'rpc' of type '{ address: string; provider?: string | undefined; archive?: boolean | undefined; }[] | undefined' is not assignable to 'string' index type '{ address: string; provider?: string | undefined; archive?: boolean | undefined; }[]'.

177         rpc?: {
            ~~~

node_modules/@chain-registry/types/types/chains.d.ts:182:9 - error TS2411: Property 'rest' of type '{ address: string; provider?: string | undefined; archive?: boolean | undefined; }[] | undefined' is not assignable to 'string' index type '{ address: string; provider?: string | undefined; archive?: boolean | undefined; }[]'.

182         rest?: {
            ~~~~

node_modules/@chain-registry/types/types/chains.d.ts:187:9 - error TS2411: Property 'grpc' of type '{ address: string; provider?: string | undefined; archive?: boolean | undefined; }[] | undefined' is not assignable to 'string' index type '{ address: string; provider?: string | undefined; archive?: boolean | undefined; }[]'.

187         grpc?: {
            ~~~~

node_modules/@chain-registry/types/types/chains.d.ts:192:9 - error TS2411: Property ''evm-http-jsonrpc'' of type '{ address: string; provider?: string | undefined; archive?: boolean | undefined; }[] | undefined' is not assignable to 'string' index type '{ address: string; provider?: string | undefined; archive?: boolean | undefined; }[]'.

192         'evm-http-jsonrpc'?: {
            ~~~~~~~~~~~~~~~~~~

node_modules/@chain-registry/types/types/chains.d.ts:197:9 - error TS2411: Property ''grpc-web'' of type '{ address: string; provider?: string | undefined; archive?: boolean | undefined; }[] | undefined' is not assignable to 'string' index type '{ address: string; provider?: string | undefined; archive?: boolean | undefined; }[]'.

197         'grpc-web'?: {
            ~~~~~~~~~~

node_modules/@chain-registry/types/types/chains.d.ts:202:9 - error TS2411: Property 'wss' of type '{ address: string; provider?: string | undefined; archive?: boolean | undefined; }[] | undefined' is not assignable to 'string' index type '{ address: string; provider?: string | undefined; archive?: boolean | undefined; }[]'.

202         wss?: {
            ~~~

node_modules/@chain-registry/types/types/chains.d.ts:207:9 - error TS2411: Property 'sidechains_rpc' of type '{ address: string; provider?: string | undefined; archive?: boolean | undefined; }[] | undefined' is not assignable to 'string' index type '{ address: string; provider?: string | undefined; archive?: boolean | undefined; }[]'.

207         sidechains_rpc?: {
            ~~~~~~~~~~~~~~

Found 7 errors in the same file, starting at: node_modules/@chain-registry/types/types/chains.d.ts:177

This is my tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "esModuleInterop": true,
    "moduleResolution": "node",
    "declaration": true,
    "outDir": "./dist",
    "strict": true,
    "preserveConstEnums": true
  },
  "include": [
    "src/**/*"
  ]
}

While the error could be fixed by the consuming project by using "skipLibCheck": true, I thought it might not be a bad idea if chain-registry type definition was also honoring strict.
The change doesn't seem too painful to make it comply.
Basically in types/types/chains.d.ts we need to change this line:

        [key: string]: {
            address: string;
            provider?: string;
            archive?: boolean;
        }[];

By that one (not the undefined at the end):

        [key: string]: {
            address: string;
            provider?: string;
            archive?: boolean;
        }[]|undefined;

By the way, are the types generated for the project or hand written? Because the address, provider, archive block could have its own subtype to be reused and make the code more readable (I would be happy to PR).

@pyramation
Copy link
Contributor

can you make a PR? we're happy to accept it — @chain-registry/types is where you can make these hand-written changes

AndreMiras added a commit to AndreMiras/chain-registry that referenced this issue Apr 20, 2024
@AndreMiras
Copy link
Author

Thanks for the guidance, I eventually looked into it, see #98
The weird thing is I couldn't reproduce the TypeScript error in strict mode while compiling from the repo even though it's the same TypeScript version that I was using when I initially got the error. And I can consistently reproduce the error when chain-registry is a project dependency. Fixed the error in the PR anyway and also applied some trivial refactoring

@mintthemoon
Copy link

Ah you had it all figured out already, I just spent about 20 minutes coming to the exact same conclusion. The fix wasn't quite so simple though, @chain-registry/interfaces turned out to not be the right package (maybe it was when the answer was given originally). I needed @chain-registry/v2-types, which in turn meant I needed @chain-registry/v2 in place of chain-registry. Then I needed to update all my code because the new package changed to camel case variable names and changed some field members.

Couldn't find any docs on v2 at all, ended up looking at the package.json in this repo to even figure out what the name of the package was, so it was a bit painful. Now though it all works, compiles fine, and I like the camel case vars much better so it's looking good!

By the way, it likely compiles in the repo but not in your project because they set "strictNullChecks": false in their tsconfig.json. This tells TypeScript not to enforce null or undefined type checks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants