Skip to content

Instantly share code, notes, and snippets.

@willowell
Created November 3, 2020 14:05
Show Gist options
  • Save willowell/c567780a687fff80190809e224510c1c to your computer and use it in GitHub Desktop.
Save willowell/c567780a687fff80190809e224510c1c to your computer and use it in GitHub Desktop.
// for instance, this TS function
import fetch from "node-fetch";
const getData = async (url: string): Promise<any | null> => {
try {
const response = await fetch(url);
const json = await response.json();
return json;
} catch (error) {
console.log(error);
return null;
}
};
// gets compiled to the following JS
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_fetch_1 = __importDefault(require("node-fetch"));
const getData = async (url) => {
try {
const response = await node_fetch_1.default(url);
const json = await response.json();
return json;
}
catch (error) {
console.log(error);
return null;
}
};
// with the following tsconfig:
{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"lib": ["ES2020"],
"allowJs": true,
"outDir": "build",
"rootDir": "src",
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"resolveJsonModule": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment