Skip to content

TypeScript

alex [dot] kramer [at] g_m_a_i_l [dot] com edited this page Jul 20, 2023 · 7 revisions

Importing untyped JS packages

See https://localhost.adobe.com:31338/templates/bfebc6a4-6d25-42ba-a52a-3eb070c98c5f

Possible solutions:

  1. Try running npm i @types/my-untyped-module to see if someone has added type declarations to DefinitelyTyped
  2. Create (or add to) a declarations.d.ts file with the line declare module "my-untyped-module" and make sure the file is in one of the directories in tsconfig.json's "include": [...].

Generate return types

Useful for avoiding manually writing out types for complicated JSON/objects. See also: https://stackoverflow.com/a/42549370

type TYPE_HELPER = ReturnType<typeof someFunction>;

Why does this compile?

interface Foo {
  twoArgs: (x: number, y: number) => void;
}

const giveMeFoo = (): Foo => {
  return {
    // Only one arg lol
    twoArgs: (x: number) => {
      console.log(`There is no "y" (R.I.P Kurt)`);
    }
  };
}

// Requires two args wtf?
giveMeFoo().twoArgs(7, 8);
Clone this wiki locally