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

@polymer/gen-typescript-declarations

v1.6.2

Published

Generate TypeScript type declarations for Polymer components.

Downloads

14,246

Readme

gen-typescript-declarations

NPM version

A library which generates TypeScript declarations for Polymer and custom elements.

How do I use the typings?

Polymer 3

Typings for Polymer 3 are included starting from version 3.0.5. To use them, install @polymer/polymer from npm, and use standard ES module import specifiers:

import {PolymerElement} from '@polymer/polymer';

class MyElement extends PolymerElement {
   ...
}

Polymer 2

Typings for Polymer 2 are included starting from version 2.4.0. To use them, install Polymer from Bower and add a triple-slash directive anywhere in your TypeScript project for the typings you require. Each HTML import from Polymer has a corresponding typings file. For example, if you depend on polymer-element.html:

/// <reference path="./bower_components/polymer/types/polymer-element.d.ts" />`

class MyElement extends Polymer.Element {
  ...
}

Alternatively, you can add the dependency to tsconfig.json in the root of your project:

{
...
	"include": [
		"src/**/*.ts",
		"src/bower_components/polymer/**/*.d.ts",
	]
}

You may also be interested in the Polymer decorators.

How do I generate new typings?

You can run this package from the command line with gen-typescript-declarations, or as a library with the generateDeclarations function.

It is recommended to integrate typings generation as part of your build/release process:

$ npm install --save-dev @polymer/gen-typescript-declarations

Add a generate-typings script to your package.json:

{
  ...
  "scripts": {
    "generate-typings": "gen-typescript-declarations"
  }
}

If you're using NPM, you can add this script to the NPM prepack script to generate and include typings in your NPM package every time you publish. Most users will want to configure their .gitignore so that the generated typings are not committed to their Git repository. In this case, take care to configure your .npmignore and/or package.json to ensure that they are included when you publish to NPM (run npm pack to check before publishing).

If you are still using Bower, ensure you run npm run generate-typings to generate the latest typings and commit them to your repository before tagging each release.

Config options

By default the gen-typescript-declarations command will read a file called gen-tsd.json in your root directory. It has the following options:

  • excludeFiles: string[]

    Skip source files whose paths match any of these glob patterns. If undefined, defaults to excluding directories ending in "test" or "demo".

  • excludeIdentifiers: string[]

    Do not emit any declarations for features that have any of these identifiers.

  • removeReferences: string[]

    Remove any triple-slash references to these files, specified as paths relative to the analysis root directory.

  • addReferences: {[filepath: string]: string[]}

    Additional files to insert as triple-slash reference statements. Given the map a: b[], a will get an additional reference statement for each file path in b. All paths are relative to the analysis root directory.

  • renameTypes: {[name: string]: string}

    Whenever a type with a name in this map is encountered, replace it with the given name. Note this only applies to named types found in places like function/method parameters and return types. It does not currently rename e.g. entire generated classes.

  • autoImport: {[modulePath: string]: string[]}

    A map from an ES module path (relative to the analysis root directory) to an array of identifiers exported by that module. If any of those identifiers are encountered in a generated typings file, an import for that identifier from the specified module will be inserted into the typings file.

Using as a module

You can also use this package as a module:

import {generateDeclarations} from 'gen-typescript-declarations';

const config = {
  "exclude": [
    "test/**",
  ],
  "removeReferences": [
    "../shadycss/apply-shim.d.ts",
  ],
  "addReferences": {
    "lib/utils/boot.d.ts": [
      "extra-types.d.ts"
    ]
  },
  "renameTypes": {
    "Polymer_PropertyEffects": "Polymer.PropertyEffects"
  }
}

// A map of d.ts file paths to file contents.
const declarations = await generateDeclarations('/my/root/dir', config);

FAQ

Why are some typings missing?

This library is based on Polymer Analyzer which has limitations in its static analysis. For certain patterns, Analyzer relies on additional JSDoc annotations.

Can I augment the generated typings with hand-written ones?

Yes, see the addReferences config option above.