closure-library icon indicating copy to clipboard operation
closure-library copied to clipboard

Connecting definitions for typing (TypeScript)

Open CoreyCole opened this issue 7 years ago • 8 comments

Using other libraries in typescript, it seems the standard post TypeScrpt 2.0 is to do something like

npm install google-closure-library @types/google-closure-library

if the types aren't shipped with the library itself. But this doesn't work as @types/google-closure-library is not registered on npm.

  • Checking out #789, it seems there are two definition files floating around (1, 2), but they have yet to be merged into DefinitelyTyped. Both of these haven't been updated in ~3 years.
  • There was a failed PR into DefinitelyTyped back in early 2016.

At the end of the day, I guess I have 2 questions:

  1. What's the suggested way for including google-closure-library type definitions in my TypeScript project right now?
  2. How could we help get these types into the DefinitelyTyped repo?

CoreyCole avatar Oct 28 '17 01:10 CoreyCole

Note: There is a @types scoped package for google-closure-compiler

CoreyCole avatar Oct 28 '17 01:10 CoreyCole

It looks like the author of one of the definitions files has an npm package for using types here with slightly awkward usage:

// Import index.ts to load type information of Closure Library.
import closure = require('./node_modules/closure-library.ts/index');
// Call register() to enable `goog.requrie()`.
closure.register();
// Then `googl.require` returns the namespace!
var Queue = goog.require('goog.structs.Queue');
// Type information of Closure Library is available in TypeScript.
var q = new Queue<number>();

CoreyCole avatar Oct 28 '17 02:10 CoreyCole

FYI I've managed to auto-generate .d.ts files by https://github.com/angular/clutz. Although, I had to fix few problems by hand.

orian avatar Oct 31 '17 23:10 orian

I don't know anything about @types - how are these typically kept up to date? What problems did you need to fix by hand?

shicks avatar Nov 08 '17 22:11 shicks

There was a list of around 7-10 changes I had to make. I thought I've pasted it in clutz issue tracker but cannot find it.

Most of them where related to wrong encapsulating. There was also a problem of incorrect inheritance transpiling for goog.Promise type (this one took me longer to figure out).

You can find the closure.d.ts file here: https://stackoverflow.com/questions/47074867/closure-compiler-failed-to-load-a-closure-library-module

I can prepare diff if your are interested (have original and modified file at home PC).

orian avatar Nov 09 '17 11:11 orian

For reference, there now is an active fork of an up-to-date version of the Closure library that has

  • Typescript definition (.d.ts) files
  • uses ES6 code instead of the custom goog.require, goog.define etc:

https://github.com/DreierF/ts-closure-library

Dan1ve avatar Apr 22 '20 13:04 Dan1ve

Thanks for the tip. That's helpful to be aware of.

For what it's worth, our thoughts in this area have changed in the last few years. Internally we use https://github.com/angular/clutz to generate .d.ts, so we're not in a huge hurry, but we are working to extract the "Good Parts" of Closure into TypeScript sources, which will then generate more idiomatic .d.ts and ES modules for NPM, etc. Since we need to move all of Google's codebase along with us, it's going to be a relatively slow process, but it's an area of active development.

shicks avatar Apr 30 '20 05:04 shicks

@shicks Hi, I was wondering if any notable progress were made since then(2020). It would be great to use high quality closure utilities on modern Typescript projects!

kkimdev avatar Sep 11 '23 15:09 kkimdev