@wc-toolkit/type-parser
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

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.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.3
603latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.3
603
1.0.2
0
1.0.1
2
1.0.00
0.0.1-beta.00

Package Sidebar

Install

npm i @wc-toolkit/type-parser

Weekly Downloads

605

Version

1.0.3

License

MIT

Unpacked Size

30.2 kB

Total Files

7

Last publish

Collaborators

  • breakstuff