Skip to content

Instantly share code, notes, and snippets.

@rabelais88
Last active August 14, 2019 03:32
Show Gist options
  • Save rabelais88/124e74d27336b49d0cce75b2f576ced1 to your computer and use it in GitHub Desktop.
Save rabelais88/124e74d27336b49d0cce75b2f576ced1 to your computer and use it in GitHub Desktop.
Typecheck in Javascript without Typescript Compilation

Typecheck in Javascript without Typescript Compilation

For anyone who wants eslint typecheck without actually using typescript, read this. I've been using this on my project to track any complicated data structure and it's been working perfectly. It supports both autocompletion and type lint. and it compiles without any performance drop as it doesn't affect the actual compilation. you don't need a complicated setup, just put *.d.ts file on your work directory.

the only downside of this, is that type conversion is awkward in this method.(the type conversion still does work though) I think the community should invent newer style of JSDOC and IDEs should support it: something similar to above(typescript + JSDOC) and leaner.

action.types.d.ts

export interface sayHelloArg {
  (msg: string, type: string): boolean;
}

export const sayHello: sayHelloArg;

export as namespace Actions;

usage

// @ts-check
/// <reference path="./action.types.d.ts" />

/** @type {Actions.sayHello} */
const sayHello(msg, type) {
  console.log(msg, type);
  return true;
}

type conversion

const greeting = (/** @type {Actions.sayHello} */(anotherFunction));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment