Skip to content

Instantly share code, notes, and snippets.

@uupaa
Last active May 3, 2023 21:20
Show Gist options
  • Save uupaa/75cd27fd4a34c7e5971eabfea57060d9 to your computer and use it in GitHub Desktop.
Save uupaa/75cd27fd4a34c7e5971eabfea57060d9 to your computer and use it in GitHub Desktop.
How to fix and fine tune TypeScript compilation errors: TS2488, TS7053

Before

  static split_semver(s:string) {
    // @arg String - "version-99.88.77"
    // @ret NumberArray - [ 99. 88, 77 ]
    try {
      const [, major, minor, patch] = Array.from(s.match(/(\d{1,2})\.(\d{1,2})\.(\d{1,2})/)); // [, "99", "88", 77" ]
      return [
        parseInt(major, 10),
        parseInt(minor, 10),
        parseInt(patch, 10),
      ];
    } catch (error) {
      return [ 0, 0, 0 ];
    }
  }
const [, major, minor, patch] = s.match(/(\d{1,2})\.(\d{1,2})\.(\d{1,2})/); // [, "99", "88", 77" ]
      ~~~~~~~~~~~~~~~~~~~~~~~
TS2488: Type 'RegExpMatchArray | null' must have a '[Symbol.iterator]()' method that returns an iterator.

After

const r:Array<string>|null = s.match(/(\d{1,2})\.(\d{1,2})\.(\d{1,2})/);
if (r) {
  const [, major, minor, patch] = r; // [, "99", "88", 77" ]
}

Before

const ORIGINS = {
  1: `https://dev.example.com`,
  2: `https://production.example.com`,
};

let mode = 1;
if (xxx) {
  mode = 2;
}
const remote_api = `${ORIGINS[mode]}/home`; 
`${ORIGINS[mode]}/home`;
   ~~~~~~~~~~~~~
TS7053: Element implicitly has an 'any' type because expression of type 'number' can't be used to index type

After

interface ORIGIN_KEYS {
  [key: number]: string;
}
const ORIGINS = {
  1: `https://dev.example.com`,
  2: `https://production.example.com`,
} as ORIGIN_KEYS;

let mode = 1;
if (xxx) {
  mode = 2;
}
const remote_api = `${ORIGINS[mode]}/home`; 

Before

const MODEL_NUMBER_ALIAS = {
  A: 1,
  B: 2,
  C: 3,
  D: 4,
  E: 5,
  F: 6,
  G: 7,
  H: 8,
};

const kit = "A";
MODEL_NUMBER_ALIAS[kit]; 
MODEL_NUMBER_ALIAS[kit];
~~~~~~~~~~~~~~~~~~~~~~~
TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type         

After

interface MODEL_NUMBER_ALIAS_KEYS {
  [key:string]: number;
}
const MODEL_NUMBER_ALIAS = {
  A: 1,
  B: 2,
  C: 3,
  D: 4,
  E: 5,
  F: 6,
  G: 7,
  H: 8,
} as MODEL_NUMBER_ALIAS_KEYS;

const kit = "A";
if (kit in MODEL_NUMBER_ALIAS) {
  MODEL_NUMBER_ALIAS[kit];
}

Before

const TAB_NAMES = {
  A: { name: "STAY", index: 0 },
  B: { name: "HOME", index: 1 },
  C: { name: "HERE", index: 2 },
};


for (const prop in TAB_NAMES) {
  const { name, index } = TAB_NAMES[prop];
}
TAB_NAMES[prop];
~~~~~~~~~~~~~~~
TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type

After

const TAB_NAMES = {
  A: { name: "STAY", index: 0 },
  B: { name: "HOME", index: 1 },
  C: { name: "HERE", index: 2 },
};
interface TAB_NAME_KEYS {
  A: { name: string, index: number };
  B: { name: string, index: number };
  C: { name: string, index: number };
  D: { name: string, index: number };
}

for (const prop in TAB_NAMES) {
//const { name, index } = TAB_NAMES[prop];
  const { name, index } = TAB_NAMES[prop as keyof TAB_NAME_KEYS];
}
@walemust
Copy link

LINES(formName: any) {
return this[formName].get('line') as FormArray;
}

Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'SalarycodeComponent'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment