Add types for Templatizer to Polymer (#27823)

* Add types for Templatizer to Polymer

Polymer was missing the typings for Templatizer, the behavior that
provides helpers for working with template elements in Polymer
components. This adds types for its public interface.

* Add tests for new templatizer types

* Fix modelForElement type to just be Polymer.Base

The closure types here: https://github.com/Polymer/polymer/blob/1.x/src/lib/template/templatizer.html incorrectly suggest that modelForElement returns an `Object<Polymer.Base>`, but in fact it just returns `Polymer.Base`
This commit is contained in:
SMores
2018-08-13 17:45:14 -04:00
committed by Alex Eagle
parent 642b4a004b
commit d742fa855e
2 changed files with 18 additions and 3 deletions

View File

@@ -263,7 +263,7 @@ declare global {
setAttribute(name: string, value: any):void;
removeAttribute(name: string):void;
observeNodes(callback: (info: ObservedNodeInfo) => void): {};
unobserveNodes(observer: {}): void;
@@ -340,6 +340,12 @@ declare global {
whenLoaded(cb: Function): void;
}
interface Templatizer {
templatize(template: HTMLTemplateElement, mutableData?: boolean): void;
stamp(model: {}): Base;
modelForElement: (elem: HTMLElement) => Base;
}
interface PolymerStatic {
Settings: Settings;
@@ -351,12 +357,14 @@ declare global {
Class(prototype: Base | { new (): Base }): CustomElementConstructor;
RenderStatus: RenderStatus
RenderStatus: RenderStatus;
ArraySplice: ArraySplice;
/** @deprecated */
ImportStatus: ImportStatus
ImportStatus: ImportStatus;
Templatizer: Templatizer;
}
}

View File

@@ -1,6 +1,8 @@
Polymer({
is: "my-element",
behaviors: [Polymer.Templatizer],
properties: {
prop1: String,
prop2: {
@@ -24,6 +26,11 @@ Polymer({
},
ready: function () {
const template = Polymer.dom(this).querySelector('template');
if (template) {
this.templatize(template);
const instance = this.stamp({item: {}});
}
this.textContent = 'My element!';
this.$.name.textContent = this.name;
this.serialize({});