Coder Social home page Coder Social logo

ajafff / ts-transform-compact-nullish-compare Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 38 KB

TypeScript transformer to make transpiled output of optional chaining and nullish coalescing less verbose

License: MIT License

TypeScript 84.86% JavaScript 15.14%

ts-transform-compact-nullish-compare's Introduction

ts-transform-compact-nullish-compare

TypeScript transformer to make transpiled output of optional chaining and nullish coalescing less verbose.

Why

TypeScript's emit for optional chaining and nullish coalescing is very verbose, but as correct as it can be:

foo?.bar;
foo ?? baz;

// is transpiled to
foo === null || foo === void 0 ? void 0 : foo.bar;
foo !== null && foo !== void 0 ? foo : baz;

// this transform converts it to the following more compact representation
foo == null ? void 0 : foo.bar;
foo != null ? foo : baz;

If your code doesn't use optional chaining or nullish coalescing on document.all you don't need the extra correctness provided by TypeScript's output.

Use cases

Minification

This reduces the size of the transpiled code. Depending on your program the reduction can be significant. Shipping less code means less network traffic, less parsing time and happier users. Execution time is probably not affected by this change.

Code coverage

The additional branches in the transpiled code increase the "Branches" count in code coverage reports. This transform removes one of those branches.

Usage with ttypescript

I wrote this transformer for use with ttypescript.

You can configure it in your tsconfig.json:

{
  "compilerOptions": {
    "plugins": [
      { "transform": "ts-transform-compact-nullish-compare", "after": true }
    ]
  }
}

Note that you can use any "type" for the transformer: the default is "type": "program", but it also works with "type": "raw" for example.

Afterwards you run ttsc as you would run tsc.

Usage with ts-loader, rollup, and TypeScript's API

This package exports the necessary factory function to create the transformer. You can use this function to plug this transformer in any major TypeScript compilation pipeline. Please refer to the API documentation of the tool you are using. Alternatively you can use ttypescript in most tools.

ts-transform-compact-nullish-compare's People

Contributors

ajafff avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.