Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@wc-toolkit/type-parser

wc-toolkit7.1kMIT1.2.0TypeScript support: included

A set of tools for retrieving and transforming data from the Custom Elements Manifest

custom-elements, custom-elements-manifest, web-components, components, cem, typescript, @wc-toolkit/type-parser

readme

workbench with tools, html, css, javascript, and typescript logos

WC Toolkit Type Parser

Using type aliases to define the types for your component’s APIs, can be helpful for keeping your code clean and organized as well as making your types reusable.

The down-side is that it can be difficult to integrate with other tooling or communicate in documentation what the available options are. This plugin parses the types so they available in a more usable format.

Installation

npm i -D @wc-toolkit/type-parser

Usage

Using type aliases to define the types for your component's APIs, can be helpful for keeping your code clean and organized as well as making your types reusable.

// my-component.ts

type Target = '_blank' | '_self' | '_parent' | '_top';

class MyLink extends HTMLElement {
  target?: Target;
}

This plugin parses the types for your component APIs in Custom Elements Manifest using the Custom Element Manifest Analyzer.

// custom-elements-manifest.config.js

import { getTsProgram, typeParserPlugin } from "@wc-toolkit/type-parser";

export default {
  ...
  // Give the plugin access to the TypeScript type checker
  overrideModuleCreation({ts, globs}) {
    const program = getTsProgram(ts, globs, "tsconfig.json");
    return program
      .getSourceFiles()
      .filter((sf) => globs.find((glob) => sf.fileName.includes(glob)));
  },

  // Add the plugin to the config
  plugins: [typeParserPlugin()],
};

Result

It doesn't overwrite the existing property, but will create a new property with the parsed type value.

// custom-elements.json

{
  "kind": "field",
  "name": "target",
  "description": "A lookup type for example",
  "attribute": "target",
  "type": {
    "text": "Target | undefined"
  },
  "parsedType": {
    "text": "'_blank' | '_self' | '_parent' | '_top' | undefined"
  }
}

Open in StackBlitz

Be sure to check out the official docs for more information on how to use this.

changelog

@wc-toolkit/type-parser

1.2.0

Minor Changes

  • 5995e59: Add config option for parsing method parameters

1.1.0

Minor Changes

  • bb312ca: Add parseObjectTypes config option to add interface/object types to CEM

1.0.3

Patch Changes

  • 5f75ef6: Excluded mode_modules from parser to prevent infinite loops

1.0.2

Patch Changes

  • 85ed983: Fixed error when trying to parse TypeScript types

1.0.1

Patch Changes

  • 1935ce1: Fixed image URL in README

1.0.0

Major Changes

  • 609c34a: Initial commit