https://github.com/acanguven/ts-safe-access
Elvis operator (aka safe navigation) implementation with typescript plugin.
https://github.com/acanguven/ts-safe-access
elvis-operator javascript typescript
Last synced: about 2 months ago
JSON representation
Elvis operator (aka safe navigation) implementation with typescript plugin.
- Host: GitHub
- URL: https://github.com/acanguven/ts-safe-access
- Owner: Acanguven
- Created: 2019-03-27T15:17:55.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-04-08T05:58:02.000Z (about 6 years ago)
- Last Synced: 2025-03-29T15:11:24.144Z (3 months ago)
- Topics: elvis-operator, javascript, typescript
- Language: TypeScript
- Homepage:
- Size: 21.5 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ts-safe-access
Hacky way to implement elvis operator (aka safe navigation) with typescript plugin.
## Module Transpiler
If you want to just get the corresponding JavaScript output given TypeScript sources.
For this you can use ts.transpileModule to get a string => string transformation like below.## Custom Compiler
If you want get custom compiler only in addition to native tsc that's run custom transformer before compilation.
Custom compiler that will take a list of TypeScript files and compile
down to their corresponding JavaScript files.### Source
```js
console.log(a.test?.way?.dd.cc?.uu);
console.log(ac?.test?.test2);
console.log(ac?.test[3]?.test2);
console.log(a?.test);
console.log(a.test?a.c?4:32:3)
```### Compiled
```js
console.log((((a.test || {}).way || {}).dd.cc || {}).uu);
console.log(((ac || {}).test || {}).test2);
console.log(((ac || {}).test[3] || {}).test2);
console.log((a || {}).test);
console.log(a.test ? a.c ? 4 : 32 : 3);
```### Install
```bash
yarn add ts-safe-access
``````bash
npm i ts-safe-access --save --dev
```### Usage
```bash
ttsc .ts
```Module transpiler example can be found [here](test/module-transpiler.ts)
Custom compiler example can be found [here](src/custom-compiler.ts)