Skip to content

Latest commit

 

History

History
33 lines (26 loc) · 854 Bytes

typescript.md

File metadata and controls

33 lines (26 loc) · 854 Bytes

TypeScript Cheatsheet

  1. use never isntead of unknown whenever possible

  2. Last resort pseudo-any types:

    export type EnumType = { [key: string]: string };
    export type ObjectType = { [key: string]: unknown | unknown[] };
  3. An example of looping through an object's keys in the actual code:

    export const as = (table: string, sqlFields: EnumType, fields: EnumType) => {
      let output: string[] = [];
    
      for (const i in sqlFields) {
        const row = `${table}.${sqlFields[i]} as ${fields[i]}`;
        output.push(row);
      }
      return output;
    };
  4. Make an object immutable

    const obj = {
      foo: 'bar'
    } as const
  5. Required<...> is the opposite of Partial<...>

  6. protected methods of TS classes are private, but accessible by child classes