angular / clutz

Closure to TypeScript `.d.ts` generator
MIT License
162 stars 60 forks source link

Filter by Custom Tags Question #1005

Closed AndriiHeonia closed 4 years ago

AndriiHeonia commented 4 years ago

Hello,

First of all, thanks for the nice library.

I have a question about Clutz. We have a JavaScript API based on JSDoc and Closure Compiler, and we would like to use Clutz to generate d.ts file. The problem is that we don't want to generate TypeScript definitions for all methods, we want to generate them only for those methods which are published in our publicly available documentation. All the methods that go to public documentation are marked by our custom @publish tag. Is there any way to filter-out Clutz output? I see that there is --skipEmitRegExp, but I don't quite understand whether it can be used to solve this particular problem and if yes then what I should provide as a value for this parameter.

Thanks in advance for the response!

rrdelaney commented 4 years ago

Hi Andrii! Can you share a little more about your setup? Clutz is pretty much only meant to generate d.ts files for TypeScript code that is eventually using goog.module somewhere, and cannot be used to generate declarations for normal TypeScript code.

AndriiHeonia commented 4 years ago

Hi Ryan, our library is written in ES5 code, it's based on Google Closure Library and minified via Google Closure Compiler. I just want to generate d.ts file to make life easier for those users who include our library into their TypeScript projects.

Let's say I have those classes:

./MyNamespace.js:

/**
 * @namespace
 * @publish
 * @name MyNamespace
 */
goog.provide('MyNamespace');

./MyUtilClass.js:

goog.require('MyNamespace');
goog.provide('MyNamespace.MyUtilClass');

/**
 * @constructor
 */
MyNamespace.MyUtilClass = function() {};

/**
 * @param {string} msg
 */
MyNamespace.MyUtilClass.prototype.log = function(msg) {
  'use strict';
  console.log(msg);
};

./MyClass.js

goog.require('MyNamespace');
goog.require('MyNamespace.MyUtilClass');

goog.provide('MyNamespace.MyClass');

/**
 * @constructor
 * @publish
 */
MyNamespace.MyClass = function() {
  /**
   * @private
   * @type {MyNamespace.MyUtilClass}
   */
  this.util_ = new MyNamespace.MyUtilClass();
};

/**
 * @publish
 * @export
 * @param {number} x
 * @param {number} y
 * @return {number}
 */
MyNamespace.MyClass.prototype.add = function(x, y) {
  'use strict';
  this.util_.log('execute add');
  return x + y;
};

/**
 * @export
 * @param {number} x
 * @param {number} y
 * @return {number}
 */
MyNamespace.MyClass.prototype.subtract = function(x, y) {
  'use strict';
  this.util_.log('execute subtract');
  return x - y;
};

When I generate d.ts file via Clutz, I get totally valid type definitions:

Screenshot 2020-05-08 at 14 15 29

The problem is that I don't want to expose methods that are not marked by our custom @publish tag. For example, I want to provide IntelliSense for MyClass#add method, but I don't want to provide it for MyClass#subtract.

Is it possible somehow to instruct Clutz to not include namespaces/classes/methods which are not marked by @publish tag into d.ts file? Or the only way to do that is to fork Clutz and change it?

Thanks for the help.

rrdelaney commented 4 years ago

I think you'll need to do additional processing to allow using that library in TypeScript. Clutz generates d.ts files for typings but cannot bridge the gap between the goog.module module system and TypeScript's. If you're pre-compiling the library for them using Closure Compiler and shipping it on npm Clutz may not work here either, the type definitions it emits are specifically for loading the library via Closure's module system 😞

Going back to the original topic, the --skipEmitRegExp is deprecated and shouldn't be used.

AndriiHeonia commented 4 years ago

Thanks for the answer, I'm closing the issue.