npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typescript-to-js

v1.0.194

Published

Typescript to javascript compiler that only drops the typescript specific keywords. No other changes are made to the code.

Downloads

13

Readme

Typescript to Javascript compiler

This Typescript to Javascript compiler only drops the Typescript specific keywords. No other changes are made to the code.

Usually, when you compile typescript code with the official Typescript compiler, the outputted Javascript code looks something like this:

"use strict";
var __assign = (this && this.__assign) || function () {
	__assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
        t[p] = s[p];
    }
    return t;
	};
	return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
	return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.compile = void 0;
var abstract_syntax_tree_1 = __importDefault(require("abstract-syntax-tree"));
var parser_1 = require("@typescript-eslint/parser");
var format_javascript_1 = require("format-javascript");
function compile(tsCodeString) {
	var typescriptAST = (0, parser_1.parse)(tsCodeString, {
		sourceType: 'module',
		range: true
	});
	return typescriptAST;
}
exports.compile = compile;

With this compiler, the result is almost the same code that you wrote, minus the typescript specific keywords. Outputs beautiful, formatted Javascript code.

Example ts input file

import fs from 'fs';
import { compile } from 'typescript-to-js';
export * from './types/index.d.ts';

export type FileArgument = string | Buffer;

function main(file: FileArgument) {
  let readFile: Buffer;

  if (file instanceof Buffer) {
    readFile = file;
  } else {
    readFile = fs.readFileSync(file);
  }
  
  const tsCode: string = readFile.toString();
  const outputFileName: string = 'output.js';
  
  const compiledJsCode: string = compile(tsCode);
  
  fs.writeFileSync(outputFileName, compiledJsCode);

  console.log('Javascript output code:\n', compiledJsCode);
}

const pathToFile = __dirname + '/src/index.ts';

main(pathToFile);

export { main };

Example js output file

Unfortunatly some white space is still lost 😔. Might add white space support in the future.

import fs from "fs";
import { compile } from "typescript-to-js";
export * from "./types/index.d.ts";

function main(file) {
  let readFile;
  if (file instanceof Buffer) {
    readFile = file;
  } else {
    readFile = fs.readFileSync(file);
  }
  const tsCode = readFile.toString();
  const outputFileName = "output.js";
  const compiledJsCode = compile(tsCode);
  fs.writeFileSync(outputFileName, compiledJsCode);
  console.log("Javascript output code:\n", compiledJsCode);
}
const pathToFile = __dirname + "/src/index.ts";
main(pathToFile);
export { main };

Usage

In Javascript:

import { compile } from 'typescript-to-js';

const tsCode = `
  export type FileArgument = string | Buffer;

  function main(file: FileArgument) {
    console.log('Hello world!', file);
  }
`;

const outputJsCodeString = compile(tsCodeString);

In Typescript:

import { compile } from 'typescript-to-js';

const tsCode: string = `
  export type FileArgument = string | Buffer;

  function main(file: FileArgument) {
    console.log('Hello world!', file);
  }
`;

const outputJsCode: string = compile(tsCode);