소스 검색

ts: ts-ignore crusade (#1174)

Paul 3 년 전
부모
커밋
d353ed133c
5개의 변경된 파일30개의 추가작업 그리고 41개의 파일을 삭제
  1. 1 2
      ts/src/coder/event.ts
  2. 6 6
      ts/src/coder/idl.ts
  3. 20 27
      ts/src/coder/instruction.ts
  4. 2 5
      ts/src/utils/pubkey.ts
  5. 1 1
      ts/src/utils/rpc.ts

+ 1 - 2
ts/src/coder/event.ts

@@ -22,7 +22,7 @@ export class EventCoder {
       this.layouts = new Map();
       return;
     }
-    const layouts = idl.events.map((event) => {
+    const layouts: [string, Layout<any>][] = idl.events.map((event) => {
       let eventTypeDef: IdlTypeDef = {
         name: event.name,
         type: {
@@ -34,7 +34,6 @@ export class EventCoder {
       };
       return [event.name, IdlCoder.typeDefLayout(eventTypeDef, idl.types)];
     });
-    // @ts-ignore
     this.layouts = new Map(layouts);
 
     this.discriminators = new Map<string, string>(

+ 6 - 6
ts/src/coder/idl.ts

@@ -60,7 +60,6 @@ export class IdlCoder {
             IdlCoder.fieldLayout(
               {
                 name: undefined,
-                // @ts-ignore
                 type: field.type.vec,
               },
               types
@@ -124,14 +123,15 @@ export class IdlCoder {
         if (variant.fields === undefined) {
           return borsh.struct([], name);
         }
-        // @ts-ignore
         const fieldLayouts = variant.fields.map((f: IdlField | IdlType) => {
-          // @ts-ignore
-          if (f.name === undefined) {
+          if (!f.hasOwnProperty("name")) {
             throw new Error("Tuple enum variants not yet implemented.");
           }
-          // @ts-ignore
-          return IdlCoder.fieldLayout(f, types);
+          // this typescript conversion is ok
+          // because if f were of type IdlType
+          // (that does not have a name property)
+          // the check before would've errored
+          return IdlCoder.fieldLayout(f as IdlField, types);
         });
         return borsh.struct(fieldLayouts, name);
       });

+ 20 - 27
ts/src/coder/instruction.ts

@@ -12,6 +12,10 @@ import {
   IdlAccount,
   IdlAccountItem,
   IdlTypeDefTyStruct,
+  IdlTypeVec,
+  IdlTypeOption,
+  IdlTypeDefined,
+  IdlAccounts,
 } from "../idl";
 import { IdlCoder } from "./idl.js";
 import { sighash } from "./common.js";
@@ -92,7 +96,7 @@ export class InstructionCoder {
     const stateMethods = idl.state ? idl.state.methods : [];
 
     const ixLayouts = stateMethods
-      .map((m: IdlStateMethod) => {
+      .map((m: IdlStateMethod): [string, Layout<unknown>] => {
         let fieldLayouts = m.args.map((arg: IdlField) => {
           return IdlCoder.fieldLayout(
             arg,
@@ -114,7 +118,6 @@ export class InstructionCoder {
           return [name, borsh.struct(fieldLayouts, name)];
         })
       );
-    // @ts-ignore
     return new Map(ixLayouts);
   }
 
@@ -245,17 +248,13 @@ class InstructionFormatter {
     if (typeof idlField.type === "string") {
       return data.toString();
     }
-    // @ts-ignore
-    if (idlField.type.vec) {
-      // @ts-ignore
+    if (idlField.type.hasOwnProperty("vec")) {
       return (
         "[" +
-        data
-          // @ts-ignore
+        (<Array<IdlField>>data)
           .map((d: IdlField) =>
             this.formatIdlData(
-              // @ts-ignore
-              { name: "", type: idlField.type.vec },
+              { name: "", type: (<IdlTypeVec>idlField.type).vec },
               d
             )
           )
@@ -263,27 +262,25 @@ class InstructionFormatter {
         "]"
       );
     }
-    // @ts-ignore
-    if (idlField.type.option) {
-      // @ts-ignore
+    if (idlField.type.hasOwnProperty("option")) {
       return data === null
         ? "null"
         : this.formatIdlData(
-            // @ts-ignore
-            { name: "", type: idlField.type.option },
+            { name: "", type: (<IdlTypeOption>idlField.type).option },
             data
           );
     }
-    // @ts-ignore
-    if (idlField.type.defined) {
+    if (idlField.type.hasOwnProperty("defined")) {
       if (types === undefined) {
         throw new Error("User defined types not provided");
       }
-      // @ts-ignore
-      const filtered = types.filter((t) => t.name === idlField.type.defined);
+      const filtered = types.filter(
+        (t) => t.name === (<IdlTypeDefined>idlField.type).defined
+      );
       if (filtered.length !== 1) {
-        // @ts-ignore
-        throw new Error(`Type not found: ${idlField.type.defined}`);
+        throw new Error(
+          `Type not found: ${(<IdlTypeDefined>idlField.type).defined}`
+        );
       }
       return InstructionFormatter.formatIdlDataDefined(
         filtered[0],
@@ -358,22 +355,18 @@ class InstructionFormatter {
     accounts: IdlAccountItem[],
     prefix?: string
   ): IdlAccount[] {
-    // @ts-ignore
     return accounts
       .map((account) => {
         const accName = sentenceCase(account.name);
-        // @ts-ignore
-        if (account.accounts) {
+        if (account.hasOwnProperty("accounts")) {
           const newPrefix = prefix ? `${prefix} > ${accName}` : accName;
-          // @ts-ignore
           return InstructionFormatter.flattenIdlAccounts(
-            // @ts-ignore
-            account.accounts,
+            (<IdlAccounts>account).accounts,
             newPrefix
           );
         } else {
           return {
-            ...account,
+            ...(<IdlAccount>account),
             name: prefix ? `${prefix} > ${accName}` : accName,
           };
         }

+ 2 - 5
ts/src/utils/pubkey.ts

@@ -81,14 +81,11 @@ const toBuffer = (arr: Buffer | Uint8Array | Array<number>): Buffer => {
 
 export async function associated(
   programId: Address,
-  ...args: Array<PublicKey | Buffer>
+  ...args: Array<Address | Buffer>
 ): Promise<PublicKey> {
   let seeds = [Buffer.from([97, 110, 99, 104, 111, 114])]; // b"anchor".
   args.forEach((arg) => {
-    seeds.push(
-      // @ts-ignore
-      arg.buffer !== undefined ? arg : translateAddress(arg).toBuffer()
-    );
+    seeds.push(arg instanceof Buffer ? arg : translateAddress(arg).toBuffer());
   });
   const [assoc] = await PublicKey.findProgramAddress(
     seeds,

+ 1 - 1
ts/src/utils/rpc.ts

@@ -82,7 +82,7 @@ async function getMultipleAccountsCore(
   if (commitment) {
     args.push({ commitment });
   }
-  // @ts-expect-error
+  // @ts-ignore
   const res = await connection._rpcRequest("getMultipleAccounts", args);
   if (res.error) {
     throw new Error(