Skip to content

added TypeScript definitions for "html" and "svg"#40

Closed
fogzot wants to merge 1 commit into
observablehq:mainfrom
fogzot:ts-definitions
Closed

added TypeScript definitions for "html" and "svg"#40
fogzot wants to merge 1 commit into
observablehq:mainfrom
fogzot:ts-definitions

Conversation

@fogzot

@fogzot fogzot commented Dec 29, 2021

Copy link
Copy Markdown

The type definitions added by this commit are pretty basic, but enough to import and use the package from TypeScript.

Comment thread src/index.d.ts
Comment on lines +2 to +13
/** Renders the specified markup as an element, text node, or null as appropriate. */
const html: {
<T extends HTMLElement | Text | null>(...args: any[]): T;
/** Renders the specified markup as a document fragment. */
fragment(...args: any[]): DocumentFragment;
};
/** Renders the specified markup as an SVG element, text node, or null as appropriate. */
const svg: {
<T extends SVGElement | Text | null>(...args: any[]): T;
/** Renders the specified markup as a document fragment. */
fragment(...args: any[]): DocumentFragment;
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly improved typings to have the proper tagged template string params (as per typings for String.raw):

Suggested change
/** Renders the specified markup as an element, text node, or null as appropriate. */
const html: {
<T extends HTMLElement | Text | null>(...args: any[]): T;
/** Renders the specified markup as a document fragment. */
fragment(...args: any[]): DocumentFragment;
};
/** Renders the specified markup as an SVG element, text node, or null as appropriate. */
const svg: {
<T extends SVGElement | Text | null>(...args: any[]): T;
/** Renders the specified markup as a document fragment. */
fragment(...args: any[]): DocumentFragment;
};
type TaggedTemplateParams = [
template: { raw: readonly string[] | ArrayLike<string> },
...substitutions: any[],
];
/** Renders the specified markup as an HTML element, text node, or null as appropriate. */
const html: {
<T extends HTMLElement | Text | null>(...args: TaggedTemplateParams): T;
/** Renders the specified markup as a document fragment. */
fragment(...args: TaggedTemplateParams): DocumentFragment;
};
/** Renders the specified markup as an SVG element, text node, or null as appropriate. */
const svg: {
<T extends SVGElement | Text | null>(...args: TaggedTemplateParams): T;
/** Renders the specified markup as a document fragment. */
fragment(...args: TaggedTemplateParams): DocumentFragment;
};

@mbostock

Copy link
Copy Markdown
Member

The implementation is now in TypeScript in #55, so we’ll have types soon. Thank you for this pull request and sorry I wasn’t able to do anything with it earlier! 🙏

@mbostock mbostock closed this May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants