ウェブサイト検索

宣言ファイルで `keyof` 文を簡略化するのを TypeScript で止める方法はありますか?


私は、LitElement の @customElement デコレーターでのタイプバグを修正しようと取り組んでいます。この問題は、TypeScript が型定義を最適化していることが原因のようです。ソースコードは次のようになっています:

export const customElement = (tagName: keyof HTMLElementTagNameMap) =>
(clazz: Constructor<HTMLElement>) => {
  window.customElements.define(tagName, clazz);
  // Cast as any because TS doesn't recognize the return type as being a
  // subtype of the decorated class when clazz is typed as
  // `Constructor<HTMLElement>` for some reason. `Constructor<HTMLElement>`
  // is helpful to make sure the decorator is applied to elements however.
  return clazz as any;
};

しかし、宣言ファイルでは出力は次のようになります:

export declare const customElement: (tagName: "object" | "a" | "abbr" | "address" | "applet" | "area" | "article" | "aside" | "audio" | "b" | "base" | "basefont" | "bdo" | "blockquote" | "body" | "br" | "button" | "canvas" | "caption" | "cite" | "code" | "col" | "colgroup" | "data" | "datalist" | "dd" | "del" | "details" | "dfn" | "dialog" | "dir" | "div" | "dl" | "dt" | "em" | "embed" | "fieldset" | "figcaption" | "figure" | "font" | "footer" | "form" | "frame" | "frameset" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "head" | "header" | "hgroup" | "hr" | "html" | "i" | "iframe" | "img" | "input" | "ins" | "kbd" | "label" | "legend" | "li" | "link" | "map" | "mark" | "marquee" | "menu" | "meta" | "meter" | "nav" | "noscript" | "ol" | "optgroup" | "option" | "output" | "p" | "param" | "picture" | "pre" | "progress" | "q" | "rt" | "ruby" | "s" | "samp" | "script" | "section" | "select" | "slot" | "small" | "source" | "span" | "strong" | "style" | "sub" | "sup" | "table" | "tbody" | "td" | "template" | "textarea" | "tfoot" | "th" | "thead" | "time" | "title" | "tr" | "track" | "u" | "ul" | "var" | "video" | "wbr") => (clazz: Constructor<HTMLElement>) => any;

アイデアは、カスタム要素を宣言するときに HTMLElementTagNameMap も拡張するということです:

declare global {
  interface HTMLElementTagNameMap {
    'my-button': MyButton
  }
}

TypeScriptが宣言ファイルのkeyof文を簡略化するのを止める方法はありますか?宣言ファイルを手動でkeyofに更新すると、すべて期待通りに動作します。

このプロジェクトのすべてのコードはhttps://github.com/Polymer/lit-elementにあります